Hey everyone. Just started learning the basics of Assembly Language from this book Programming From The Ground Up, and from the first example, ran into some trouble. Would appreciate a little help if you could. I am running Open Solaris through VM ware. So I wrote the following in the vi editor and saved it as exit.s

#Purpose: Simple program that exits and returs status back to kernel mode
#Input:        None
#Output:        returns a status code. THis can be viewed by typing echo $? after running the program

#Variables:    %eax holds the system call number
#              %ebx holds the return status

.section .data
.section .text
.globl _start
_start:

movl $1, %eax  #This is a linux kernel command number for exiting a program (system call number)

movl $0, %ebx  #THis is a status number we will return to the OS. Change this around and it will return different status to echo $?

int $0x80      #tHIS wakes up the kernel to run the exit command.

I saved what i wrote. Did as exit.s -o  exit.o followed by ld exit.o -o exit. The book states that by running the program is supposed to not do anything bu exit, however, i get a message stating Segmentation Fault (core dumped). And after the echo $? the value of 139 is returned, even though I am supposed to get 0. Once again, any help would be appreciated. Thank you.
Posted on 2010-06-27 22:51:24 by Algasar
Don't quote me on this, but I believe that OpenSolaris uses the BSD system calling convention where arguments are pushed onto the stack instead of placed into registers. Try this:

.section .text
.globl _start
_start:

pushl $0 # Exit Code
movl $1, %eax # SYS_EXIT
int $0x80 # Interrupt Kernel
Posted on 2010-06-28 01:50:30 by Synfire
I think BSD (and maybe Solaris) is even more different than that. Wants an extra parameter on the stack - a "return address". You can provide a "stub" that just does the "int 0x80" and returns (and call it), or push a "dummy return address" - %eax will do - and do the "int 0x80" inline. (this puts the parameter in the same place for "exit()" and "sys_exit")

First steps are the hardest, Algasar. Courage!

Best,
Frank

Posted on 2010-06-29 00:13:24 by fbkotler