Skip to content

Instantly share code, notes, and snippets.

@ChanderG
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChanderG/7df27036866db9c80a8f to your computer and use it in GitHub Desktop.
Save ChanderG/7df27036866db9c80a8f to your computer and use it in GitHub Desktop.
Hello World for syscall in assembly
#Hello World example of syscall
#What follows after this are not comments. It is actually instructions to the cpp - c pre processor to include those files and definition for converting this to .s file.
#include <asm/unistd.h>
#include <syscall.h>
#define STDOUT_FILENO 1
.file "syscall.S"
.section .rodata
L1:
.string "Testing syscalls \n"
L2:
.text
.globl _start
_start:
movl $(SYS_write), %eax # a constant defined to indicate write to
movl $(STDOUT_FILENO), %ebx # the standard output = 0
movl $L1, %ecx # starting address of string to be written
movl $(L2-L1), %edx # size of item to be written
int $128
movl $(SYS_exit), %eax
movl $0, %ebx
int $128

#Very Important Instructions for this file

A simple hello world x86 program demonstrating syscalls.

###Build Instructions: You need to run the cpp to convert the .S file to .s file. Then instead of manually assembling and linking the file, you can run cc as shown.

###Main vs _start Here note the use of the 'dont include std lib' flag. This is because we do not have a main but a direct _start. If you want to use main instead, replace all instances of _start with main and remove this flag. It should work the same.

/lib/cpp -m32 syscall.S syscall.s
cc -nostdlib -m32 syscall.s
./a.out

The expected output is :

Testing syscalls 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment