21,459
社区成员
发帖
与我相关
我的任务
分享
#include "stdio.h"
#include "stdlib.h"
typedef unsigned long DWORD;
//pn[0], the lowest 32bits
//pn[1], the middle 32bits
//pn[2], the highest 32bits
void uint96_to_string(DWORD *pn, char *szString)
{
_asm
{
push esi
push edi
push ecx
mov esi,pn
mov edi,szString
mov ecx,10
jmp check_n
div_h_32bit:
mov eax,dword ptr [esi+8]
div ecx
mov dword ptr [esi+8], eax
div_m_32bit:
mov eax,dword ptr [esi+4]
div ecx
mov dword ptr [esi+4], eax
div_l_32bit:
mov eax,dword ptr [esi]
div ecx
mov dword ptr [esi], eax
add dl,'0'
mov byte ptr [edi],dl
inc edi
check_n:
xor edx,edx
cmp [esi+8],0
jnz div_h_32bit
cmp [esi+4],0
jnz div_m_32bit
cmp [esi],0
jnz div_l_32bit
//now, the whole 96bit number is full 0
cmp edi,szString
jne invert_n
//if uint96 is 0, then edi == szString, in this case, we need put '0' to szString and return
mov byte ptr [edi],'0' //put 0 to buffer
mov byte ptr [edi+1],0 //c language string terminating symbol
jmp thisExit
invert_n:
mov byte ptr [edi],0 //c language string terminating symbol
dec edi //the edi point to the tail of string
mov esi,szString //the esi point to the head of string
jmp invert_cmp
invert_loop:
mov al,[esi]
mov dl,[edi]
mov [edi],al
mov [esi],dl
inc esi
dec edi
invert_cmp:
cmp esi,edi
jb invert_loop
thisExit:
pop ecx
pop edi
pop esi
}
}
void test_uint96_to_string()
{
DWORD n[3];
char result[32];
int case_id=1;
//case 1:
n[0]=0;
n[1]=0;
n[2]=0;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 2:
n[0]=9;
n[1]=0;
n[2]=0;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 3:
n[0]=10;
n[1]=0;
n[2]=0;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 4:
n[0]=0xffffffff;
n[1]=0;
n[2]=0;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 4:
n[0]=0;
n[1]=1;
n[2]=0;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 6:
n[0]=1;
n[1]=1;
n[2]=0;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 7:
n[0]=0;
n[1]=0;
n[2]=1;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 8:
n[0]=1;
n[1]=0;
n[2]=1;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
//case 9:
n[0]=0xffffffff;
n[1]=0xffffffff;;
n[2]=0xffffffff;;
uint96_to_string(n,result);
printf("run case %d,result is %s\n",case_id,result);
case_id++;
}
int main(int argc, char* argv[])
{
test_uint96_to_string();
}
;conver a number result to output_buffer and fill '$' after number string
;function:
; conver 32 bit integer result to string
;input parameter:
; none, always convert varible result
;output parameter
; di: The address of output buffer
output_num proc
push di ;di is the address of out_buff
mov bx,10
convert_high:
xor dx,dx
mov ax,word ptr [result+2]
div bx
mov word ptr [result+2],ax
convert_low:
mov ax,word ptr [result]
div bx
mov word ptr [result],ax
add dl,'0' ;save (result % 10)+'0' to out_buffer
mov byte ptr [di],dl
inc di ;di point next position
cmp_r:
cmp word ptr [result+2],0
jnz convert_high
mov dx,0
cmp word ptr [result],0
jnz convert_low
inv_string: ;swap string head and string tail
pop si ;now si is the address of out_buff
dec di ;the di point to the last char
mov bx,di
inv_loop:
mov al,[si] ;swap char
mov ah,[di]
mov [di],al
mov [si],ah
inc si
dec di
cmp_head_tail:
cmp si,di
jb inv_loop
mov byte ptr [bx+1], '$' ; For dos ah=09, string output, the terminal char must be '$'
ret
output_num endp