Hi I'm new to x86 assembly and I wanted to try implementing a basic print function that takes the address of the string to be printed and its length that executes the write interrupt on it.
I did the following:
.global _start
.intel_syntax
.section .text
# void print(char *msg, int len)
print:
push %bp
mov %bp, %sp
mov %ecx, [%bp+6] # len
mov %edx, [%bp+4] # msg
mov %eax, 4
mov %ebx, 1
int 0x80
pop %bp
ret 4
_start:
push [msg]
push 13
call print # print(msg, len)
mov %eax, 1
mov %ebx, 0
int 0x80
.section .data
msg:
.ascii "Hello, world!\n"
I compiled it with the following commands:
as test.asm --32 -o test.o
gcc -m32 test.o -nostdlib
The resulting error is of course s Segfault but I don't understand where I'm going wrong.
Any advice?