I’m pretty new to assembly and am experimenting with calling C standard library functions from my assembly code. I’m on 64 bit windows 11, and use nasm syntax.
I have three files which call putchar and pass 77 (the ascii code for capital M).
The first and last work, but not the second.
The three files are called test1.asm, test2.asm, and test3.asm:
test1.asm
bits 64
default rel
extern ExitProcess
extern putchar
global main
section .text
main:
; sub 32 to allocate shadow space
sub rsp, 32
; call putchar with ascii character M
mov rcx, 77
call putchar
; call ExitProcess with exit code 0
xor rcx, rcx
call ExitProcess
test2.asm
bits 64
default rel
extern ExitProcess
extern putchar
global main
section .text
main:
; sub 8 to allocate memory for a qword
; sub 32 to allocate shadow space
sub rsp, 40
; copy 77 into rbx
; copy rbx (77) into [rbp-08]
mov rbx, 77
mov [rbp-08], rbx
; call putchar passing the value at [rbp-08] (77)
mov rcx, [rbp-08]
call putchar
; call ExitProcess with exit code 0
xor rcx, rcx
call ExitProcess
test3.asm
bits 64
default rel
extern ExitProcess
extern putchar
global main
section .text
main:
; sub 32 to allocate shadow space
sub rsp, 32
call secondary
; zero rcx to call ExitProcess
xor rcx, rcx
call ExitProcess
secondary:
push rbp
mov rbp, rsp
; sub 8 to allocate memory for a qword
; sub 8 so that the final total is 16-byte aligned
; sub 32 to allocate shadow space
sub rsp, 48
; save rbx because its volatile
; (although technically this shouldnt be needed bc main doesnt use rbx)
push rbx
; copy 77 into rbx
; copy rbx (77) into [rbp-08]
mov rbx, 77
mov [rbp-08], rbx
; call putchar passing the value at [rbp-08] (77)
mov rcx, [rbp-08]
call putchar
; restore rbx
pop rbx
add rsp, 48
pop rbp
ret
I assembled and linked them using the following commands in the cmd command prompt:
D:Desktoptemp>nasm -fwin64 test1.asm
D:Desktoptemp>nasm -fwin64 test2.asm
D:Desktoptemp>nasm -fwin64 test3.asm
D:Desktoptemp>ld C:WindowsSystem32kernel32.dll C:WindowsSystem32msvcrt.dll test1.obj -o a1.exe
D:Desktoptemp>ld C:WindowsSystem32kernel32.dll C:WindowsSystem32msvcrt.dll test2.obj -o a2.exe
D:Desktoptemp>ld C:WindowsSystem32kernel32.dll C:WindowsSystem32msvcrt.dll test3.obj -o a3.exe
And I got this as output when I ran each of the executables (also in the cmd command prompt):
D:Desktoptemp>a1.exe
M
D:Desktoptemp>a2.exe
D:Desktoptemp>a3.exe
M
D:Desktoptemp>
I can’t figure out why test2.asm doesnt print out M but is instead blank.
In my mind, test1 does the same thing as test2 but in less steps by moving 77 directly into rcx, and test3 does the same thing as test2, only in a seperate function.
Both test1 and test3 work, so why doesn’t test2?
This question is similar to mine:
Can’t call `putchar` in assembly
But does not help me to understand why test1 and test3 do happen to work correctly, whereas test2 does not (assuming the fact that I don’t see any output from test2 is indeed because “the libc’s IO package performs buffered IO and you do not flush the buffer of stdout before terminating your program”).
resoinaut is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1