πŸ“— -> 11/7: C to Assembly


[Lecture Slide Link]

🎀 Vocab

❗ Unit and Larger Context

Small summary

βœ’οΈ -> Scratch Notes

C to Assembly:

While Loop

Rewrite in terms of while(true)

while (true) {
  if (i >= val) {
    break;
  }
  code
}
while_start:
# i >= val == i - val >= 0
cmpl %eax, %ecx, #ecx - eax == i - val
jge while_end
//Code
jpm while_start
while_end:

Do while loop is easy, check at the end and just jump back to start if true, else continue

For Loop

Rewrite in terms of code breaking inside if code is true. ie in the for loop gets pushed outside

for(int i = 0;true; ++i){
  if( i >= val){ //negation of orig condition
    break  
  }
  code
}
# Assume i is in ECX and val is in EAX
movl $0, %ecx # i = 0
for_start:
 #i >= val == i - val >= 0
 cmpl %eax, %ecx #i - val
 jge for_end
 Code
 incl %ecx #++i
 jmp for_start
for_end:

If else If

if(i < 10){
   Code1
} else if(i < 20){
  Code 2
} else{
  Code 3
}
If1: # i < 10 == i - 10 < 0 | neg = i - 10 >= 0
  Cmpl $10, %ecx
  jge if2
  Code1
  jmp end_else
If2: # i < 20 == i -20 < 0 | neg = i - 20 >= 0
  Cmpl $20, %ecx
  jge else_start
  Code2
  jmp end_else
else_start:
  Code3
end_else:

If a and b and c gets compiled to:
if a:
if b:
if c:
code
Write assembly code like this, break if not the conditions

If a or b or c
Jump to code if any of the comparisons are true, but if not true jump don’t take the jumps and continue execution until a jump to the not code block

Advance Indexing

d(o,i,s):

  • Displacement: Constant Expresion
  • Offset: Register
  • Index: Register
  • Scale: 1,2,4,8
    *(d + o + i*s) == memory[d + o + i*s]

Resources

  • Put useful links here

Connections

  • Link all related words