mov eax, 5
mov ebx, 5
cmp eax, ebx
jne label
no :(
If your counter is in ecx, which command allows you to iterate through a loop ecx number of times?
loop loop_label
Assume x is in eax and y is in ebx.
How do you implement if (x==y) in assembly?
cmp eax, ebx
jne exit_branch
; if statement body
exit_branch:
Where is the return address stored during a procedure call?
The stack
What does the command jecxz do?
jumps if the ecx register contains 0
mov eax, 10
mov ebx, 15
cmp eax, ebx
jbe label
yes :)
for (int i=5; i > 0; i--)
{body}
mov ecx, 5
loop_label:
; body
loop loop_label
Assuming balance is in eax, write the following in assembly:
if (balance>=0)
{ do A}
else
{ do B}
cmp eax, 0
jnge do_else
; do A
jmp end_if
do_else:
; do B
end_if:
What 2 things happen when you make a procedure call?
1. The return address is pushed on to the stack
2. The program jumps to the start of the procedure
What is the difference between lea source and mov source?
lea loads the address of the source while mov loads the value of the source
mov eax, 7
mov ebx, 10
cmp eax, ebx
jge label
no :(
Assuming x is in eax, write the following loop in assembly:
while (x< 10)
{body}
while_loop:
cmp eax, 10
jnl exit_loop
; body
exit_loop:
Assume x is in eax, y is in ebx, and z is in ecx.
How do you write if (x>y || z==0) in assembly?
cmp eax, ebx
jng exit_branch
cmp ecx, 0
jne exit_branch
; if statement body
What is a return address?
The address/location of instruction after a call
What does the command cmp eax, [ebx] do?
Compares the value in eax to the value at the address in ebx
mov eax, 0x00
mov ebx, 0x00
jnz label
no, cmp subtracts ebx from eax and sets the flags based on the difference. The difference is 0, so the zero flag is set. "Jump if not zero" is false.
Assuming x is in eax and y is in ebx, write the following in assembly:
while (x>100 && y<5)
{body}
while_loop:
cmp eax, 100
jng exit_loop
cmp ebx, 5
jnl exit_loop
; body
exit_loop:
What does the following assembly code evaluate?
cmp eax, ebx
jg body
cmp ecx, 0
je body
jmp endIf
body:
; do something
jmp endIf
endIf:
if eax > ebx or ecx==0
In assembly, what do the following commands do:
push ebp
mov ebp, esp
Stores the location of the old stack pointer and moves it to the current location of ESP (establishes the "bottom" of the stack for a procedure)
Which instruction can be added to movsb to move multiple bytes?
the REP command repeats a statement until ecx equals 0
mov eax, -1
mov ebx, 0
cmp eax, ebx
jns label
no, cmp subtracts ebx from eax and sets the flags based on the difference. The difference is -1, so the sign flag is set. "Jump if not signed" is false.
while_loop:
cmp eax, 0
jne exit_loop
; body
exit_loop:
What does the following assembly code evaluate?
cmp eax, ebx
jg greater
je equal
jmp less
greater:
; do A
jmp end
equal:
; do B
jmp end
less:
; do C
end:
if eax>ebx: do A
else if eax==ebx: do B
else: do C
Once a procedure has been called, we can access the first argument with [ebp+8]. What does this mean?
8 bytes above the base pointer on the stack
What's the difference between sar and shr?
sar is an arithmetic shift (extends the sign bit) and shr is a logical shift (pads with 0s)