pathterminuspages/snippets/aboutcontactabout me

Syscall - write from asm

14.01.2018

Linux provides a set of system calls aviable for each process. Some of the most used are:

NumberNameDescription
0readRead file
1writeWrite file
2openOpen file
3closeClose file
4statGet info about file
9mmapMap memory page to file
12brkReset top of the heap
32dup2Copy file descriptor
33pauseSuspend process until signal arrives
37alarmSchedule delivery of alarm signal
39getpidGet process id
57forkCreate process
59execveExecute program
60_exitTerminate process
61wait4Wait for a process to terminate
62killSend signal to a process

Now using the write we can perform a syscall asm-wise:

#include <stdio.h> __asm( //setup string named msg ".section .data;" "msg: .ascii \"Hello from global space!\n\";" "msg_end: .equ len, msg_end - msg;" ".section .text;" ); int main(){ __asm( //call write "movq $1,%rax;" "movq $1,%rdi;" "movq $msg,%rsi;" "movq $len,%rdx;" "syscall;" //call exit "movq $60,%rax;" "movq $0,%rdi;" "syscall;" ); }

So we have chosen the syscall write with number 1, that number goes to rax. The next thing is to man 2 write which yields:

WRITE(2) Linux Programmer's Manual NAME write - write to a file descriptor SYNOPSIS #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count);

So we need arg fd - that is a file descriptor. STDIO has fd = 1. We need a buffer. That we have set up in the first __asm. Last we need a number of bytes to be written. This also has been set up in the first call to __asm. The last part of the code is a similar call to exit - let's man:

_EXIT(2) Programmer's Manual NAME _exit, _Exit - terminate the calling process SYNOPSIS #include <unistd.h> void _exit(int status); #include <stdlib.h> void _Exit(int status);

As before we man for the type signature. In this case we need rax to be 60, as that it the number of the syscall, and we need to pass the exit status as 0. Pretty low level!

CommentsGuest Name:Comment: