- Fill the table with 0's and 1's for four flags: CF (carry flag), SF (sign or negative number flag), ZF (zero flag), and OF (positive/negative overflow):
| Number Type | %rdi/edi/di | %rsi/esi/si | Instruction | CF | SF | ZF | OF |
(a) | Unsigned | 0xFFFE | 0x4 | addw %di, %si | | | | |
(b) | Unsigned | 0xFFFE | 0x4 | addl %edi, %esi | | | | |
(c) | Signed two's complement | 0xFFFE | 0x2 | addw %di, %si | | | | |
(d) | Signed two's complement | 0xFFFE | 0x2 | addl %edi, %esi | | | | |
(e) | Signed two's complement | 0xFFFFFFFF | 0x80000000 | addl %edi, %esi | | | | |
(f) | Signed two's complement | 0xFFFF | -0xFFFF | subl %si, %di | | | | |
(g) | Signed two's complement | 0xFFFFFFFE | 0x7FFFFFFE | subl %esi, %edi | | | | |
(h) | Unsigned | 0xF | 0xFF | shlq 64, %rdi | | | | |
-
In the assembly code below, jump targets are encoded in PC-relative form, in other words, target addresses are computed relative to the memory address of the next instruction. Following the Practice problem 3.15 in page 330, fill the table with target addresses by replacing aaaaaa-eeeeee with memory addresses:
| Address | Instructions in Hexa | --- | Assembly Instructions |
(a) | ab1234: | 74 08 | | je aaaaaa |
ab1236: | 48 89 d0 | | mov %rdx,%rax |
(b) | abcdef: | 7c 07 | | jl bbbbbb |
abcdf1: | 48 39 f7 | | cmp %rsi,%rdi |
(c) | cccccc: | 7d 11 | | jge 0x123456 |
dddddd: | 48 85 ab | | test %rdi,%rdi |
(d) | ab01f0: | 7f 2f ff ff | | jg eeeeee |
ab01f4: | 48 39 d6 | | mov %rdx,%rsi |
- This problem checks if you followed the reverse logic usage for jumping to else part. Fill in the C code for the assembly. Again use the jump-to-else-part reverse logic we used in class and also by following the practice problem 3.18 of page 213.
reverse_logic:
cmpq %rsi, %rdi
jge .L3
cmpq %rdx, %rdi
jle .L4
movq %rdx, %rax
subq %rdi, %rax
ret
.L4:
leaq (%rdi,%rdx), %rax
ret
.L3:
cmpq %rdx, %rsi
jle .L7
movq %rdx, %rax
subq %rsi, %rax
ret
.L7:
leaq (%rsi,%rdx), %rax
ret
long reverse_logic(long x, long y, long z)
{
long result;
...
...
return result;
}
- Do problem 3.60.
- This problem checks again if you followed the jump to middle logic used for a while loop. Fill in the C code for the assembly below. Again use the reverse logic we used in class and also following the practice problem 3.18 of page 213.
loop_while_hw5:
.LFB0:
movl $1, %eax
jmp .L2
.L3:
movq %rdi, %rdx
subq %rsi, %rdx
addq %rdx, %rax
addq $1, %rsi
.L2:
cmpq %rdi, %rsi
jl .L3
rep ret
long loop_while_hw5(long a, long b)
{
long result = _____________;
while (___________) {
result = _________________________;
b = ______________________________;
}
return result;
}
- For a C function switch_hw5 below and the assembly code along with a jump table, fill in the missing parts of the C code. See page 233 and Practice Problem 3.31 on how the labels in case statements and the Jump Table can be related.
(a) C Code
void switch_hw5(long a, long b, long c, long *dest)
{
long val;
switch(a) {
case _____:
val = _______________________;
break;
case _____:
c = _______________________;
/* Fall through */
case _____:
val = _______________________;
break;
case _____:
case _____:
val = _______________________;
break;
default:
val = _______________________;
}
return val;
}
(b) Assembly Code
switch_hw5:
.L3:
movq %rdx, %rax
subq %rdi, %rax
ret
.L5:
movq %rsi, %rdx
salq $4, %rdx
addq %rsi, %rdx
.L6:
movq %rdx, %rax
xorb $-1, %al
ret
.L7:
leaq (%rdx,%rsi), %rax
sarq $4, %rax
ret
.L2:
leaq (%rdi,%rsi), %rax
ret
(c) Jump Table
.L4:
.quad .L3
.quad .L5
.quad .L2
.quad .L6
.quad .L2
.quad .L7
.quad .L2
.quad .L7