Link to some information of flags when opening files. Jakob's slide should also be updated we needed information:
https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/bits/fcntl-linux.h.html
http://www.pythontutor.com/c.html#mode=display
void swap(int* a, int* b) { int* temp_1; int* temp_2; temp_1 = a; temp_2 = b; a = temp_2; b = temp_1; } void swap_pointers(int** a, int** b) { int* temp_1; int* temp_2; temp_1 = *a; temp_2 = *b; *a = temp_2; *b = temp_1; } int main() { int x = 6; int *y; int z[10]; int **a; y = malloc(sizeof(int)); *y = 8; z[0] = 1; z[1] = 2; z[2] = 3; z[3] = 4; z[4] = 5; a = malloc(sizeof(int *) * 10); a[0] = y; z[1] = y; a[1] = 2; int* temp; temp = malloc(sizeof(int)); *temp = 42; a[2] = temp; free(temp); a[4] = &z[3]; a[5] = y; swap(a[4], a[5] ); swap_pointers(&a[4], &a[5] ); return 0; }
SOURCES = hello.c OBJS := hello.o CFLAGS = -g all: hello hello: $(OBJS) cc $(OBJS) $(CFLAGS) -o hello clean: rm -f $(OBJS) hello
note: The function print_rax is used from another file (provided by Jakob) and is linked by the 'ld' command when linking this file. note: The function is calculating the Fibonacci "(%rax + 1)"-th number. This means if %rax has value 5 we calculate the 6-th Fibonacci number.
.global _start _start: movq $5, %rax # find the 6th Fibonacci number movq $1, %rbx # Set to 1 movq $0, %rcx # Set to 0 call print_rax call FibFunction movq %rdx, %rax call print_rax # Syscall calling sys_exit movq $60, %rax # rax: int syscall number movq $0, %rdi # rdi: int error code syscall FibFunction: push %rbp movq %rsp, %rbp movq $0, %rdx # If needed: # subq #pusha # Save all general purpose registers # If needed: #movq (rbp+16 + 8*0), %rax # first parameter on stack #movq [rbp+16 + 8*1], %rbx # second parameter on stack #movq [rbp+16 + 8*2], %rcx # third parameter on stack addq %rcx, %rdx addq %rbx, %rdx # rdx hold the n-th Fibonacci number movq %rbx, %rcx # If we want an itteration more, this is now n-2 movq %rdx, %rbx # This will be n - 1 subq $1, %rax cmpq $0, %rax je done call FibFunction done: #popa # Restore all general purpose registers pop %rbp ret
.section .data .align 16 # add padding bytes so the next thing is 16-byte aligned ss: .single 0.1, 0.2, 0.3, 0.4 ds: .double 1.1, 1.2 .section .text .global _start _start: movaps ss , %xmm0 movapd ds , %xmm1 # Syscall calling sys_exit movq $60, %rax # rax: int syscall number movq $0, %rdi # rdi: int error code syscall
Save the file as xmm.s and compile it like this:
as -g -o xmm.o xmm.s
Then link is by
ld -o xmm.bin xmm.o
Now we can run the binary with gdb:
gdb ./xmm.bin
Set a breakpoint:
break _start
Start the program:
run
Run the first instructions to move the numbers into the registers:
nexti
nexti
Now print the content of the xmm0 and xmm1 registers:
p $xmm0
p $xmm1
You should now be able to see that the values we have specified in the file now is in the registers.
Continue to use the nexti command untill you can see that the program exited normally.