The QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

You can find a lot of system call info in the C headers.

by rpgfan3233 (no login)

From /usr/include/sys/syscall.h:
/usr/include/asm/unistd.h - syscall numbers
/usr/include/bits/syscall.h - defines C equivalents to the syscall #defines from unistd.h

Also, there is /usr/include/unistd.h, which shows:
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

It also defines file permission checks: R_OK = 4, W_OK = 2, X_OK = 1, F_OK = 0 (is_readable, is_writeable, is_executable, file_exists). Those alone should keep you busy enough. The only things you need to know are:
1) when you execute a system call (calling a function like printf (e.g. "pushl $0b110 #yes that is binary <newline> pushl $.format <newline> call printf") is simply a wrapper for executing certain system calls, such as write (syscall no. 4), except for the fact that using "call <function>" uses the stack, so the stack pointer should be reset to its original state before the function call. If you pushed 2 things using pushl, you have to add 8 to %esp because the size of a longword is 4 bytes and 2 * 4 = 8.
2) %eax contains the syscall number. %ebx, %ecx and %edx vary depending on the syscall. For example, for writing "Hello World!\n" to STDOUT:

### start of program ###
## filename: test.s ##
##
## assembly instructions: ##
## as -o test.o test.s ##
## ld -s -o test test.o ##
##
## run with ./test ##
## check the program's exit status using echo $? (not a typo) ##
##
## write returns the number of characters written ##
## (don't forget newlines and at least 1 null terminator) ##
##
# data section
.section .data
.hellostring:
.string "Hello World!\n"
.hellostring_len = . - .hellostring # I think this is a relative thing, where the first "." after the "=" sign represents the current address

# syscalls we'll use
.equ SYS_EXIT, 1 # exit syscall
.equ SYS_WRITE, 4

# file descriptors we'll use
.equ STDOUT_FILENO, 1

# text section (actual program goes here)
.section .text
.globl _start
_start:
movl $SYS_WRITE, %eax
movl $STDOUT_FILENO, %ebx
movl $.hellostring, %ecx # %ecx contains the data stored in ".hellostring"
movl $.hellostring_len, %edx # %edx contains the length of the data to print
int $0x80 # kernel interrupt

# The return value of the call to write is stored in %eax.
# We want to return the success of the call to write as
# the exit status of the program, which is %ebx
movl %eax, %ebx
movl $SYS_EXIT, %eax
int $0x80 # kernel interrupt again
### end of program ###


BTW, all of that was from memory. I'm not exactly sure if this is how it works, but it seems like syscalls like write(int file_descriptor, const void* buffer, int bytes) and exit(int status) use successive registers, where %eax is the syscall number and everything else. Note that the write syscall returns -1 if there was an error. In some implementations, when the number of bytes to write is 0, an error may be returned rather than 0 for 0 bytes written. However, I also remember reading that if there aren't enough registers, the stack is used instead. As a final note, I recommend checking out the book "Programming From the Ground Up"[1] if you are serious about Linux assembly. It uses AT&T syntax (GAS, the GNU Assembler), but converting the code to NASM or FASM syntax shouldn't be too hard. Just remember to assemble to aout format (nasm -f aout <file>.asm (if you use ELF, you end up with the ELF header which increases the filesize by about 100 bytes) and then link with ld as shown above. I converted the above program to NASM in less than 5 minutes after a quick refresher on the syntax (and I didn't mess up the string part either. I knew it was "Hello World!",0Ah,$ . ;-)
GAS uses the '$' prefix on a variable to denote a literal value. Without the prefix, it is a memory address, or rather a pointer to a memory address.


[1] - http://savannah.nongnu.org/projects/pgubook/ ##the actual book is a PDF >:(

Posted on Feb 16, 2007, 4:37 AM

Respond to this message   

Return to Index


Response TitleAuthor and Date
Thanks for that rpg on Feb 16
 * Good reference!rpgfan3233 on Feb 20

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums