CS350 Intro Computer Systems Homework

Homework 5 on Control

  1. 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/siInstructionCFSFZFOF
    (a)Unsigned0xFFFE0x4addw %di, %si
    (b)Unsigned0xFFFE0x4addl %edi, %esi
    (c)Signed two's complement0xFFFE0x2addw %di, %si
    (d)Signed two's complement0xFFFE0x2addl %edi, %esi
    (e)Signed two's complement0xFFFFFFFF0x80000000addl %edi, %esi
    (f)Signed two's complement0xFFFF-0xFFFFsubl %si, %di
    (g)Signed two's complement0xFFFFFFFE0x7FFFFFFEsubl %esi, %edi
    (h)Unsigned0xF0xFFshlq 64, %rdi
  2. 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:
    AddressInstructions in Hexa---Assembly Instructions
    (a)ab1234:74 08je aaaaaa
    ab1236:48 89 d0mov %rdx,%rax
    (b)abcdef:7c 07jl bbbbbb
    abcdf1:48 39 f7cmp %rsi,%rdi
    (c)cccccc:7d 11jge 0x123456
    dddddd:48 85 abtest %rdi,%rdi
    (d)ab01f0:7f 2f ff ffjg eeeeee
    ab01f4:48 39 d6mov %rdx,%rsi
  3. 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;
    }
      
  4. Do problem 3.60.
  5. 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;
    }
      
  6. 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