Question: Fix the current assembly sum_diff function: .globl sum_diff sum_diff: pushq %rbp movq %rsp, %rbp subq $16, %rsp movl (%rdi), %eax # a addl (%rsi), %eax
Fix the current assembly sum_diff function:
.globl sum_diff
sum_diff: pushq %rbp movq %rsp, %rbp subq $16, %rsp
movl (%rdi), %eax # a addl (%rsi), %eax # + b movl %eax, (%rdi) # a = sum
movl (%rdi), %eax # sum cmpl (%rsi), %eax # < a ? setb %dl movb %dl, (%rcx) # *psum = 0 or 1
movl (%rdi), %eax # sum subl (%rsi), %eax # - b movl %eax, (%rsi) # b = diff
movl (%rdi), %eax # sum subl (%rsi), %eax # - b movl %eax, (%rsi) # b = diff
movl (%rdi), %eax # sum subl (%rsi), %eax # diff = a - b cmpl (%rsi), %eax # > a ? seta %dl movb %dl, (%rcx) # *pdiff = 0 or 1
addq $16, %rsp popq %rbp ret
From the original C code:
# void sum_diff(unsigned *a, unsigned *b, char *psum, char *pdiff){ # unsigned sum = a + b; # unsigned diff = a - b; # *psum = 0; # *pdiff = 0; # if(sum < a) # *psum = 1; # if(diff > a) # *pdiff = 1; # *a = sum; # *b = diff; # }
Where sum_diff function receives four parameters: two pointers to type unsigned integer and two pointers to type unsigned char. The function sets the first char to 1 if the sum of the two unsigned integers overflows, 0 otherwise. The function sets the second char to 1 if the difference of the two unsigned integers overflows, 0 otherwise. The function also stores the sum of the two unsigned integers in the first unsigned integer and the difference in the second unsigned integer.
And the test cases and their expected CORRECTED outputs are:
| ./prog2 sum_diff 25 12 | sum(25, 12) = 37 overflow? 0, diff(25, 12) = 13 overflows? 0 |
| ./prog2 sum_diff 12 25 | sum(12, 25) = 37 overflow? 0, diff(12, 25) = 4294967283 overflows? 1 |
| ./prog2 sum_diff 3000000000 2000000000 | sum(3000000000, 2000000000) = 705032704 overflow? 1, diff(3000000000, 2000000000) = 1000000000 overflows? 0 |
| ./prog2 sum_diff 2000000000 3000000000 | sum(2000000000, 3000000000) = 705032704 overflow? 1, diff(2000000000, 3000000000) = 3294967296 overflows? 1 |
Because I am currently outputting these INCORRECT outputs:
| ./prog2 sum_diff 25 12 | sum(25, 12) = 37 overflow? 0, diff(25, 12) = 15775243 overflows? 0 |
| ./prog2 sum_diff 12 25 | sum(12, 25) = 37 overflow? 0, diff(12, 25) = 15775256 overflows? 0 |
| ./prog2 sum_diff 3000000000 2000000000 | sum(3000000000, 2000000000) = 705032704 overflow? 0, diff(3000000000, 2000000000) = 2015775232 overflows? 0 |
| ./prog2 sum_diff 2000000000 3000000000 | sum(2000000000, 3000000000) = 705032704 overflow? 0, diff(2000000000, 3000000000) = 3015775232 overflows? 0 |
The sum_diff assembly function should use different arithmetic/logical instructions for integer and floating-point types
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
