开发者

simple assembly syscall not working

开发者 https://www.devze.com 2023-02-07 02:49 出处:网络
I\'m following those video tutorials on the assembly language. I\'m basically trying to work out the \"hello world\" asm example.

I'm following those video tutorials on the assembly language. I'm basically trying to work out the "hello world" asm example. Here is what I've got:

.data
str:
        .ascii "Hello World"

.text
.globl _start

_start:
        movl    $4, %eax
        movl    $1, %ebx
        movl    $str, %ecx
        movl    $11, %edx
        int     $0x80

        movl    $1, %eax
        movl    $0, %ebx
        int     $0x80

This compiles just fine but when I run it, no text is printed to the terminal. I have no idea what I'm doing wrong. Whatever value I mov into the ecx register makes no difference, nothing happens.

Also, other question, how does the syscall work when it call the int 0x80 instruction ? Some 开发者_运维技巧data has been moved to the registers but when we get to the syscall, it doesn't "use" any of those values. Does it go and get what has been moved to those registers on it's own ?

Some system info that might be helpful:

dominic-@-freebsd-9 ~/dev/asm/tutorial > uname -a
FreeBSD freebsd-9 5.5-RELEASE FreeBSD 5.5-RELEASE #0: Tue May 23 14:58:27 UTC 2006     root@perseus.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386


I think you need Linux emulation for this to work. Take a look into Developer's Handbook for details.


FreeBSD has the more 'usual' calling convention, where the syscall number is in eax, and the parameters are on the stack

  • http://asm.sourceforge.net/intro/hello.html

check out the freebsd section of this hello world in assembly totorial

I go between ubuntu and netbsd and that helped me write assembly for both

*bsd uses a stack to store the arguments of the file des and the length of bytes to write, in linux it's just kept in the registers eax and ebx which is linux style, as you have in your example.

in linux: _start:;tell linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel

in freebsd:

_syscall:
    int0x80;system call
    ret 

_start:;tell linker entry point

    pushd word len;message length
    pushd word msg;message to write
    pushd word 1;file descriptor (stdout)
    move ax,0x4;system call number (sys_write)
    call _syscall;call kernel

use the stack version for syscalls on *bsd


You need to have %ebx set to 0 (stdout). Your current value (1) means stdin.

0

精彩评论

暂无评论...
验证码 换一张
取 消