I have written a simple Assembly program (Intel syntax / NASM & ld) to print a single character using the interruption 80h (0x80) on Linux. It works fine if I specify an address in memory where that character is located at, but it doesn't seem to work using the stack to store it. I'm setting ECX (the register that stores the address of the string to be printed) to the value of ESP after pushing 0x41 (hex code for the capital letter A) to the stack. It compiles fine but does nothing when I run it.
Below we have the code that worked and the code that didn't.
Works:
global _start ;entry point
section .data
a: db "A"
section .text
_start:
mov eax, 4 ;sys_write
mov ebx, 1 ;stdout
mov ecx, a ;address of that char
mov edx, 1 ;length of the string
int 0x80
Doesn't work:
global _start ;entry point
section .text
_start:
mov eax, 4 ;sys_write
mov ebx, 1 ;stdout
push 0x00000041
mov ecx, esp
mov edx, 1 ;length of the string
int 0x80
Any thoughts? I'm using the following script to compile, link and run the application:
nasm -felf64 main.s -o main.o
ld main.o -o main
./main
PS: I do know I'm using ELF64 as the output format for NASM. If I try ELF32, ld doesn't work. That's irrelevant in this situation.
User contributions licensed under CC BY-SA 3.0