I'm learning x86 assembly on Linux and this is the first program I wrote, so I'm looking for all kinds of ways to improve it.
; Build using these commands:
; nasm -f elf printenv.asm
; ld -o printenv printenv.o -m elf_i386 -s
section .data
section .bss
section .text
global _start
_start:
pop ecx ; load argument count
lea esp,[esp+ecx*4+4] ; move past every argument and null ptr
PopEnv:
pop esi ; get next env variable
cmp esi,0 ; null means end
je Exit ; exit if null
; calculate length
mov ecx,0ffffffffh ; max length
xor eax,eax ; search for null byte
mov edi,esi ; where to look for
repne scasb ; search
dec edi ; edi points one past the null
mov byte [edi],0ah ; replace null with EOL
sub edi,esi ; length
mov edx,edi ; set arg for sys_write
inc edx ; include EOL
; print env to console
mov eax,4 ; sys_write
mov ebx,1 ; stdout
mov ecx,esi ; location
int 80h ; call
cmp eax,edx ; check return value
jb ExitFailure ; error
jmp PopEnv ; get another env
Exit:
mov eax,1 ; sys_exit
mov ebx,0 ; EXIT_SUCCESS
int 80h ; make the call
ExitFailure:
mov eax,1 ; sys_exit
mov ebx,-1 ; EXIT_FAILURE
int 80h ; make the call