Created
October 13, 2017 02:46
-
-
Save loganintech/d59c3938d8d1cbcc02df2b48aafb0d7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/syscall.h> | |
#define STRING "Hello, World!\n" | |
.globl main | |
.type main, @function | |
main: | |
jmp call_addr /* jmp to call addr to get the address of STRING */ | |
pop_addr: | |
/* | |
Objective: | |
int fd = open("txt", 0, 0); | |
size_t size = read(fd, buffer, 256); | |
write(1, buffer, size); | |
System call for Linux with x86 architecture gets syscall number from eax, | |
and its 1st argument from ebx, | |
2nd argument from ecx, | |
3rd argument from edx, | |
4th argument from edi, | |
5th argument from esi, ... | |
*/ | |
// int fd = open("txt", 0, 0); | |
// build a string "txt" using push! | |
push $0x00747874 | |
// move that string to ebx | |
mov %esp, %ebx | |
// make ecx zero | |
mov $0, %ecx | |
// make edx zero | |
mov $0, %ecx | |
// move $SYS_open to eax | |
mov $SYS_open, %eax | |
// syscall! | |
int $0x80 | |
// size_t size = read(fd, buffer, 256); | |
// move eax to ebx (to set fd!) | |
mov %eax, %ebx | |
// set ecx as esp (stack buffer) | |
mov %esp, %ecx | |
// set edx as 256 | |
mov $256, %edx | |
// set eax as $SYS_read | |
mov $SYS_read, %eax | |
// syscall! | |
int $0x80 | |
// write(1, buffer, size); | |
// set edx as eax, to set it as size read | |
mov %eax, %edx | |
// set ecx as esp (stack buffer) | |
mov %esp, %ecx | |
// set ebx as 1 (stdout!) | |
mov $1, %ebx | |
// set eax as $SYS_write | |
mov $SYS_write, %eax | |
// syscall! | |
int $0x80 | |
// Do you want to exit at here? | |
// set eax to be 1 ($SYS_exit) | |
mov $1, %eax | |
// syscall! | |
int $0x80 | |
int $0x80 /* syscall */ | |
call_addr: | |
call pop_addr /* call popaddr, at the stack, it will store | |
the address of (call popaddr) + 5, which is the | |
address of STRING */ | |
.string STRING |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment