Re: Formatting in assembly



On 30 Mar 2006 16:27:15 -0800, "James Daughtry"
<mordock32@xxxxxxxxxxx> wrote:

Okay, this is a combination of "What constitutes good formatting in
assembly?" and "Hey Betov, how does this strike you?". Here's some
RosAsm code formatted with Betov's suggestions. I'll eat my hat and say
that the flow is definitely easier to follow:

main:
call 'msvcrt._write',1,msg1,D$msg1_len
call 'msvcrt._read',0,buf,blen

dec eax | mov D$n,eax

call atoi,buf,D$n
call itoa,result,eax,D$n
call 'msvcrt._write',1,result,D$n
ide_exit

msvcrt._write(1,msg1,D$msg1_len);
etc

.....

/* if return 1 OK, if return 0 error
itoa:
j=[s+4]; a=[s+8]; c=[s+12]; b=10;
c<>1; <#.ce; =#.c5;
..c0: { r=0; div b; /* r=a%10, a=a/10
rl+='0'; *j=rl; ++j; a==0#.c2;
..c1: --c#.c0;
}
..c2: i=[s+4]; B*j=0; c==0#.ce; --j;
#.c4;
..c3: {al=*i; *j<->al; *i=al; --j; ++i;
..c4: i<j#.c3
}
a=1;#.cf;
..c5: B*j=0;
..ce: a=0
..cf:
ret

NOT TESTED

this is the traslation in the nasmw syntax

; if return 1 OK, if return 0 error
itoa:
mov edi, [esp+4]
mov eax, [esp+8]
mov ecx, [esp+12]
mov ebx, 10
cmp ecx, 1
jb .ce
jz .c5
..c0: ; r=a%10, a=a/10
mov edx, 0
div ebx
add dl, '0'
mov [edi], dl
inc edi
cmp eax, 0
je .c2
..c1:
dec ecx
jnz .c0

..c2:
mov esi, [esp+4]
mov byte[edi], 0
cmp ecx, 0
je .ce
dec edi
jmp short .c4
..c3:
mov al, [esi]
xchg [edi], al
mov [esi], al
dec edi
inc esi
..c4:
cmp esi, edi
jb .c3

mov eax, 1
jmp short .cf
..c5:
mov byte[edi], 0
..ce:
mov eax, 0
..cf:
ret


itoa:
mov edi,D$esp+4 | mov eax,D$esp+8 | mov ecx,D$esp+12

; Convert number to string
L0: jecxz L1>
; Chop LSD, add to string as ASCII
mov edx,0 | mov ebx,10 |div ebx
add dl,'0' | mov B$edi,dl

inc edi | dec ecx
test eax,eax | jnz L0<
L1:

mov esi,D$esp+4

; 123 -> '321', reverse string to fix
L0: test esi,edi | jne L1>
; Exchange two characters
mov al,B$esi | xchg B$edi,al | mov B$esi,al

inc esi | dec edi
jmp L0<
L1:
ret

for me this itoa seems wrong at last because don't end the string with
\0 and not has some return error code

Now, I was curious if this style of formatting would translate
effectively into other assemblers. The only real issue I saw was that
RosAsm allows multiple instructions on a single line, while, NASM, for
example, does not (that I'm aware of). So an immediate disadvantage
would be that common instructions can no longer be logically grouped on
a line.
.



Relevant Pages