CS208 Introduction to Computer Systems Friday, 2 February 2024 + Office hours today - Busy 10-11 - Available 9:35-10:00 and during convo + Today: key ideas for the new assignment + x86-64 syntax reminder - Intel syntax - used by Windows compilers - used in nearly all online documentation - OP destination, source - AT&T - used by Unix compilers (notably gcc) - OP source, destination - Get a cheat sheet from somebody using gcc + Instructions of interest - Resources - instruction references - cheat sheets - Easy ones - mov -- it should have been named copy - add - sub - neg -- one operand, set to -1 * original - Example mov $0x5, %rsi mov $0x9, %rax sub %rax, %rsi puts -4 = 0xFFFFFFFFFFFFFFFC into %rsi jl .L8 jumps to label .L8 because -4 < 0 and "jl" means "jump if less than" Note: jl is looking at the SF dest = dest - source (see CMU cheat sheet) %rsi = %rsi - %rax modifies the EFLAGS register according to the final result look at SUB on the instruction ref (felixcloutier.com/x86/) look at FLAGS on Wikipedia SF bit gets set to 1 because the result was < 0 ZF bit gets set to 0 because the result was != 0 - Other essentials - cmp a, b computes b - a, sets flags SF, ZF, etc., then throws the result away - test a, b computes (a bitwise-AND b), sets flags SF, ZF,... then throws the result away - jcc "jump condition code", jump or not based on flags jl jge ... - lea "load effective address" sometimes used to compute addresses of things (pointer arithmetic) often used to just do arithmetic - push - pop - call - ret - Others that show up in this assignment - set - or - movzb - movs - sal + Addressing modes - Immediate $0x36 --> the number 0x36 - Register %rax --> the number in %rax - Register Indirect (%rax) --> the thing in memory at address %rax - Register Indirect with offset 0x12(%rax) --> the thing in memory at address 0x12 + %rax - Indexed (a, b, c) --> a + b*c (a, b) --> a + b*1 = a + b - Indexed with offset d(a, b, c) --> a + b*c + d d(a, b) --> a + b*1 + d = a + b + d ===== puzzle0.asm thinking function1: movl %edi, %eax --- %edi is the first parameter, no %esi so this is certainly a 1-param function retval (eax) = param testl %edi, %edi --- bitwise AND of the param with itself so SF and ZF get set based on the param js .L3 --- jump to .L3 iff the param < 0 .L2: ret .L3: negl %eax --- retval = retval * -1 jmp .L2 ---- int f(int edi) { // absolute value ("abs") int eax = edi; if (edi < 0) { eax = eax * -1; } else { } return eax; }