Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given this c program and its assembly, modify the assembly code so that it retur

ID: 3731845 • Letter: G

Question

Given this c program and its assembly, modify the assembly code so that it returns 3*x + 7*y + 14*z using only shift and add (or subtract). Do not use the leal or imull instruction.

#include #include "calc . h" int calc(int x, int y, int z)í return 3*x 2*y 15*z; //Assembly file "calc.c" .text globl calc .type calc, @function calc: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 16(%ebp), %ecx leal (%edx,XedX, 2), %eax movl 12 (%ebp), %eax leal (%edx,%eax,2), %eax movi %ecx, %edx sall $4, %edx subl %ecx, %edx add! %edx, %eax popl %ebp ret .size calc, .-calc .ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3" .section .note.GNU-stack," ",@progbits

Explanation / Answer

Whenver you want to perform multiply operation, say multiply a number by n using only shift operators and add/sub instructions then represent n as addition/subtraction of powers of two. E.g. in this example we wanted to multiply z by 15 which is 16-1 => 24 - 20. Then first left shift by 4 places and then by 0 places and subtract these two values. The result will be equivalent to direct multiplication by 15.

calc:

pushl %ebp

movl %esp, %ebp

movl 8(%ebp), %edx ; x

movl 16(%ebp), %ecx ; z

movl 12(%ebp), %eax ; y

; multiply x by 3, left shift by 2(multiply by 4(2^2)) then subtract x

movl %edx, %ebx

sall $2, %ebx ; x<<2

subl %ebx, %edx ; x<<2 - x

; multiply y by 2 by left shift by 1

sall $1, %eax ; y<<1

; multiply z by 15, left shift by 4(multiply by 16(2^4)) then subtract z

movl %ecx, %ebx

sall $4, %ebx ; z<<4

subl %ebx, %ecx ; z<<2 - z

; add all the temp results together

addl %edx, %eax

addl %eax, %ecx

movl %ecx, %eax ; save final result into eax

popl %ebp

ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote