asked 98.3k views
1 vote
Transcribe the C code given below into assembly but you are not allowed to use multiplication, only use the power of 2 through addition (

12a = 8a + 4a
You can get to 8a by doing:
doubled = a + a
quad = doubled + doubled
times_eight = quad + quad
From the above you can see that we can multiple by 8 by using 3 adds if we do it in a clever way. (note that 8 = 23)
times_twelve = times_eight + quad
Altogether that is just 4 adds to multiply by 12 instead of 12.)
Here is the C code itself that you need to transcribe into assembly code
#include
#include
int main() {
int favoritePointer; // declare a pointer to int and name it favorite
printf("Enter your favorite number: "); // ask the user to enter their favorite number
scanf("%d", &favoritePointer); // allocate memory for favoritePointer before using scanf. Read the user's input and store the address in favoritePointer.
printf("\\"); // print a new line
printf("%d\\", favoritePointer); // print the value stored at the address favoritePointer
int favoriteTimes9 = favoritePointer * 9; // multiply the value stored at the address in favoritePointer by 9
printf("%d\\", favoriteTimes9); // print the result of favoriteTimes9
int favoriteTimes24 = favoritePointer * 24; //// multiply the value stored at the address in favoritePointer by 24
printf("%d\\", favoriteTimes24); // display the output to teh user
return 0;
}
Here is the assembly starter code:
; Author: Megan Avery Spring 2023
; This is the starter code for Project 4
%include "asm_io.inc"
segment .data
;
; initialized data is put in the data segment here
;
prompt db "Enter your favorite number: ", 0 ; creating a string for use later
segment .bss
;
; uninitialized data is put in the bss segment
;
favorite resd 1
segment .text
global asm_main
asm_main:
enter 0,0 ; setup routine
pusha
;
; don't edit anything between lines 20 and 23
;
; STARTER CODE, DO NOT EDIT
mov eax, prompt
call print_string ; print out the prompt defined in the .data segment
call read_int ; value entered by user now lives in EAX
call print_nl ; print a new line character
call print_int ; print value in EAX, currently what user entered
; YOUR CODE HERE
;
; don't edit anything below this line
;
popa
mov eax, 0 ; return back to C
leave
ret

1 Answer

5 votes

Answer:

section .data

prompt db "Enter your favorite number: ", 0

fmt db "%d", 0

section .bss

favorite resd 1

section .text

global asm_main

asm_main:

enter 0,0

pusha

mov eax, prompt

call print_string

call read_int

call print_nl

call print_int

; Your multiplication code here

mov ebx, eax ; Save the input value

; Calculate 8 times the input (8a = a + a + a + a + a + a + a + a)

add eax, eax ; a + a

add eax, eax ; a + a + a + a

mov ecx, eax ; Store 8a in ECX

; Calculate 4 times the input (4a = a + a + a + a)

mov eax, ebx ; Restore the input value

add eax, eax ; a + a

mov edx, eax ; Store 4a in EDX

; Calculate 12 times the input (12a = 8a + 4a)

add eax, ecx ; 12a = 8a + 4a

; Printing the results

call print_nl

call print_int

popa

mov eax, 0

leave

ret

Step-by-step explanation:

I don't if this is the answer you need

answered
User Alexkr
by
8.9k points

Related questions