assemblytutorials icon indicating copy to clipboard operation
assemblytutorials copied to clipboard

Lesdon #5 - function sprint changes eax

Open ruafelianna opened this issue 2 years ago • 1 comments

;------------------------------------------
; 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

ruafelianna avatar Jul 07 '22 00:07 ruafelianna

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

DGivney avatar Jul 19 '22 04:07 DGivney