Formatting in assembly
- From: "James Daughtry" <mordock32@xxxxxxxxxxx>
- Date: 30 Mar 2006 16:27:15 -0800
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:
[push | push #1 | #+1]
[call | push #L>2 | call #1]
[exit | push 0 | call 'Kernel32.ExitProcess']
[ide_exit | call 'msvcrt._fgetchar' | exit]
__________________________________________________________
; User messages
[msg1: B$ 'Enter a number: ' msg1_len: len]
; User data
[blen 100]
[buf: ? #blen n: ? result: ? #blen]
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
atoi:
mov esi,D$esp+4 | mov ecx,D$esp+8
mov eax,0
L0: jecxz L1>
; Convert from ASCII, add to LSD
movzx ebx,B$esi | sub ebx,'0'
mov edx,10 | mul edx | add eax,ebx
inc esi | dec ecx
jmp L0<
L1:
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
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.
I don't think that's too much of a disadvantage, though it does
lengthen the code and readers have to rely more on empty lines to
separate logical groups. Here's the same program translated to NASM:
[section .data]
msg1: db 'Enter a number: ',0
msg1_len: equ $-msg1
[section .bss]
blen: equ 100
buf: resb blen
n: resd 1
result: resb blen
[section .text]
global _main
extern _write, _read
%macro scall 2-*
%define _func %1
%assign __params %0 - 1
%assign __params __params * 4
%rep %0 - 1
%rotate -1
push %1
%endrep
call _func
add esp,__params
%endmacro
_main:
scall _write,1,msg1,msg1_len
scall _read,0,buf,blen
dec eax
mov dword[n],eax
scall atoi,buf,dword[n]
scall itoa,result,eax,dword[n]
scall _write,1,result,dword[n]
mov eax,0
ret
atoi:
mov esi,[esp+4]
mov ecx,[esp+8]
mov eax,0
.L0: jecxz .L1
; Convert from ASCII, add to LSD
movzx ebx,byte[esi]
sub ebx,'0'
mov edx,10
mul edx
add eax,ebx
inc esi
dec ecx
jmp .L0
.L1:
ret
itoa:
mov edi,[esp+4]
mov eax,[esp+8]
mov ecx,[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 byte[edi],dl
inc edi
dec ecx
test eax,eax
jnz .L0
.L1:
mov esi,[esp+4]
; 123 -> '321', reverse string to fix
.L2: test esi,edi
jne .L3
; Exchange two characters
mov al,byte[esi]
xchg byte[edi],al
mov byte[esi],al
inc esi
dec edi
jmp .L2
.L3:
ret
The formatting works, but not quite as well. Ironically, this is
similar to my first attempts at writing assembly where I tried to
format the code like I would C. I rather like it, but I'm curious what
everyone else around here thinks about formatting assembly source code.
The sky is the limit, how do you feel assembly should be formatted for
readability? :-)
Cheers!
.
- Follow-Ups:
- Re: Formatting in assembly
- From: ¬a\\/b
- Re: Formatting in assembly
- From: Betov
- Re: Formatting in assembly
- From: sevagK
- Re: Formatting in assembly
- From: Chewy509
- Re: Formatting in assembly
- Prev by Date: Re: RosAsm User Interface
- Next by Date: Re: GDI can be fast!
- Previous by thread: Cherche des experts en assembleur
- Next by thread: Re: Formatting in assembly
- Index(es):
Relevant Pages
|