c library tutorial added

This commit is contained in:
2019-08-13 19:59:14 +02:00
parent 26379ad01f
commit f2cad14426
2 changed files with 77 additions and 0 deletions

72
x86_64/6_clib.asm Normal file
View File

@@ -0,0 +1,72 @@
; We need to tell it about main when using c lib functions
; else it will complain about it during linking
global main
; c library symbols for the compiler (linker doesn't care)
extern printf
extern atoi
section .data
; Strings to printf'
fmt: db '%d + %d = %d',0xA,0x0 ; append with newline and null terminator
usageMsg: db 'usage: %s first_number second_number',0xA,0x0
section .text
; Note that this will be called by some external function (actually,
; the compiler will generate its own _start that eventually calls this)
main:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
; main takes an int and a char pointer as arguments
mov r12, rdi
mov r13, rsi
; This segfaults when moved below the line "jne .fail"
; Why?
sub rsp, 8 ; allocate 8 bytes = 2 integers
cmp r12, 3 ; argc == 3
jne .fail
mov rdi, qword [r13 + 8] ; argv[1]
call atoi
mov dword [rsp], eax ; store integer 1
mov rdi, qword [r13 + 16] ; argv[2]
call atoi
mov dword [rsp + 4], eax ; store integer 2
; add the two numbers
mov ecx, dword [rsp]
add ecx, dword [rsp + 4]
; print the result
mov rdi, fmt
mov esi, [rsp]
mov edx, [rsp + 4]
call printf
; main returns an integer
xor rax, rax
jmp .end
.fail:
mov rdi, usageMsg
mov rsi, [r13] ; argv[0] = the executable name
call printf
mov rax, 1
.end:
pop r15
pop r14
pop r13
pop r12
pop rbx
mov rsp, rbp
pop rbp
ret

View File

@@ -2,7 +2,9 @@ CC = gcc
AS = nasm
LD = ld
CFLAGS = -g -no-pie
ASFLAGS = -g -F dwarf -f elf64
LFLAGS =
ASSEMBLIES = $(wildcard *.asm)
PROGRAMS = $(patsubst %.asm, bin/%, ${ASSEMBLIES})
@@ -24,5 +26,8 @@ build:
build/%.o: %.asm
${AS} ${ASFLAGS} $< -o $@
bin/6_clib: build/6_clib.o
${CC} ${CFLAGS} $< -o $@ ${LFLAGS}
bin/%: build/%.o
${LD} $< -o $@