Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions