V Lecture 20 — addressing and arithmetic in x86 assembly
V hw5 questions
V should freemem make sure it’s argument is a valid pointer previously allocated by getmem?
* no, freemem should check for NULL, but otherwise just assume the argument is valid
V how do you store the memory for each block?
* the memory is not stored explicitly anywhere
* when you have a pointer to a block header, you actually have a pointer to the start of a larger block of memory
* you just happen to be using the first 16 bytes of that memory as your header struct
V how does the free list get started?
* declare global variable that points to first block on the list in mem_impl.h
* check if this pointer is NULL at the start of getmem
* if so, use malloc to create the first block on the list
V how does bench work?
* for each trial (of which there are ntrials), generate a random number to decide if this trial will be a call to getmem or freemem
* there should be a pctget chance of choosing a getmem call
* if it’s getmem, generate a second random number to choose whether the request will be large or small, and a third random number to choose the exact size
V if it’s freemem, generate a second random number to choose which previous allocation to free
* if there are no previous allocations, do nothing, but still count it as a trial
V exercise:
* memory:
address value
0x100 0xFF
0x104 0xAB
0x108 0x13
0x10C 0x11
* registers
register value
%rax 0x100
%rcx 0x1
%rdx 0x3
V what is the value of the following operands:
V %rax
* 0x100 // register
V 0x104
* 0xAB // absolute address
V $0x108
* 0x108 // immediate
V (%rax)
* 0xFF // address 0x100
V 4(%rax)
* 0xAB // address 0x104
V understanding swap
V complete addressing mode
V D(Rb,Ri,S)
* D: constant integer offset (“displacement”)
* Rb: base register (any)
* Ri: index register (any except %rsp)
* S: scale (1, 2, 4, or 8)
* retrieves value at memory location Rb + S•Ri + D
V parameters have default values when omitted
V examples:
* for (Rb, Ri), S=1 and D=0
* for D(, Ri, S), Rb=0
* for D(Rb), Ri=0
V exercise:
V using the memory and register values from previous exercise, what is the value of following operands:
V 9(%rax, %rdx)
* 0x11 // 0x9 + 0x100 + 0x3 = address 0x10C
V 260(%rcx, %rdx)
* 0x13 // 260 = 0x104, 0x104 + 0x1 = 0x3 = address 0x108
V 0xFC( , %rcx, 4)
* 0xFF // 0xFC + 0x4 * 0x1 = address 0x100
V (%rax, %rdx, 4)
* 0x11 // 0x100 + 0x4 * 0x3 = address 0x10C
V leaq Src, Dest
* load effective address
* Src is address expression (any of those discussed above)
* Dest is a register
* sets Dest to address computed by expression
* example: leaq (%rdx,%rcx,4), %rax
V uses:
V computing address without actually going to memory
* e.g., translation of p = &x
V arithmetic of the form x + k*i
* for k = 1, 2, 4, or 8
V Does leaq go to memory? NO
* it just “does the math” to computer an address
V leaq vs movq example
V arithmetic
V unary instructions (take one argument)
* incq D: D D + 1
* decq D: D D - 1
* negq D: D -D
* notq D: D ~D
V binary instructions (take two arguments)
* addq S,D: D D + S
* subq S,D: D D - S
* imulq S,D: D D * S
* xorq S,D: D D ^ S
* orq S,D: D D | S
* andq S,D: D D & S
* salq k,D: D D << k
* sarq k,D: D D >> k
V long arith (long x, long y, long z) {
long t1 = x+y;
long t2 = z+t1;
long t3 = x+4;
long t4 = y * 48;
long t5 = t3 + t4;
long rval = t2 * t5;
return rval;
}
* arith:
leaq (%rdi,%rsi), %rax
addq %rdx, %rax
leaq (%rsi,%rsi,2), %rdx
salq $4, %rdx
leaq 4(%rdi,%rdx), %rcx
imulq %rcx, %rax
ret
V exercise:
* using the same memory and register values as previous exercises
V determine the destination and the value that will be stored at that destination for the following instructions
V addq %rcx, (%rax)
* 0x100 (0xFF + 0x1) stored at 0x100
V subq %rdx, 4(%rax)
* 0xA8 (0xAB - 0x3) stored at 0x104
V imulq $16, (%rax, %rdx, 4)
* 0x110 (0x10 * 0x11) stored at 0x10C
V incq 8(%rax)
* 0x14 stored at 0x108
V decq %rcx
* 0x0 (0x1 - 0x1) stored at %rcx
V subq %rdx, %rax
* 0xFD (0x100 - 0x3) stored at %rax
*