Why doesn't pushing a character to the stack without an explicit nul-char look like an underfined behaviour?

44 Views Asked by At

The following snippet comes from the lesson 7 on asmtutor.com :

;------------------------------------------
; void sprintLF(String message)
; String printing with line feed function
sprintLF:
    call    sprint
 
    push    eax         ; push eax onto the stack to preserve it while we use the eax register in this function
    mov     eax, 0Ah    ; move 0Ah into eax - 0Ah is the ascii character for a linefeed
    push    eax         ; push the linefeed onto the stack so we can get the address
    mov     eax, esp    ; move the address of the current stack pointer into eax for sprint
    call    sprint      ; call our sprint function
    pop     eax         ; remove our linefeed character from the stack
    pop     eax         ; restore the original value of eax before our function was called
    ret                 ; return to our program

sprint is a subroutine that executes an equivalent of strlen on the string pointed to by eax and then uses the obtained result to perform a write with the whole string. The purpose of the subroutine from this snippet is to perform an sprint but also systematically print a newline feed by pushing a 0Ah on the stack and then performing a sprint on esp (after a mov eax, esp).

But putting a nul-character after the 0Ah on the stack wasn't explicitly made. And yet, sprint will rely on a strlen to know the size to pass to the write syscall.

question :

Is it normal to not explicitly put a nul-char after the linefeed character on the stack? And how is it not a segfault risk/undefined behaviour (as I'd fear the strlen to not stop without nul-char and thus causing problems)?

0

There are 0 best solutions below