DGivney / assemblytutorials

This project was put together to teach myself NASM x86 assembly language on linux.
https://asmtutor.com/
654 stars 117 forks source link

Lesdon #5 - function sprint changes eax #42

Closed ruafelianna closed 1 year ago

ruafelianna commented 2 years ago
;------------------------------------------
; void sprint(String message)
; String printing function
sprint:
    push    edx
    push    ecx
    push    ebx
    push    eax
    call    slen

    mov     edx, eax
    pop     eax

    mov     ecx, eax
    mov     ebx, 1
    mov     eax, 4
    int     80h

    pop     ebx
    pop     ecx
    pop     edx
    ret

Function sprint changes EAX register by doing

mov eax, 4

but doesn't restore it after that.

I suggest to add one more

; after pop eax
push eax

and then

pop eax
; before pop ebx
DGivney commented 2 years ago

Hey there!

Thanks for getting in touch.

I agree, it is a little bit odd to push something onto the stack and then not replace it at the end of the function. However, my thinking at time was to allow the developer to read the return value from sys_write (which could return -1 to indicate an error).

Responsibility then was left to the developer to restore the original value from the stack if they required it.

https://man7.org/linux/man-pages/man2/write.2.html