Skip to content

Instantly share code, notes, and snippets.

@xxc3nsoredxx
Created July 11, 2020 06:36
Show Gist options
  • Save xxc3nsoredxx/ada6f3fe82277753ab7f48dfc56f06db to your computer and use it in GitHub Desktop.
Save xxc3nsoredxx/ada6f3fe82277753ab7f48dfc56f06db to your computer and use it in GitHub Desktop.
cc.asm
; linux x86 assembly example of a standard calling convention
; tweak the linker flag if not on intel cpu
; assemble: nasm -felf32 cc.asm
; link: ld -m elf_i386 cc.o -o cc
; run: ./cc
; exit code: echo $?
; exposes the startpoint of the program to the linker
global _start
; code sits in the text section
section .text
; C equivalent
; int func (int a, int b, int c) {
; return (a + b) * c;
; }
func:
push ebp
mov ebp, esp
mov eax, [ebp + 8] ; eax is often used for return value
; get a
add eax, [ebp + 12] ; get and add b
mul DWORD [ebp + 16] ; get and multiply c
; automatically multiplies into eax
mov esp, ebp
pop ebp
ret
_start:
; let's call func(1, 2, 3)
push DWORD 3 ; parameters are passed in reverse
push DWORD 2
push DWORD 1
call func ; eax now should have 9
mov ebx, eax ; set eax as the exit code
mov eax, 1 ; sys_exit is syscall 1
int 0x80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment