Question

Implement the functionality of "main" function for the following C code in MIPS assembly. Comment your...

Implement the functionality of "main" function for the following C code in MIPS assembly. Comment your code including which registers represent the variables in the main function. Include the .data and the .text sections. Do not write the bit_count function.

int main() {

int count = 149;

int num = 432;

if (count + num > 534){

count = bit_count(num);

printf("%d bits to store the number", count);

}else{

printf("cannot count bit in %d",)

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For above problem:

As ques says no need to write bit_count function so i assume to print the count value only.

.data
msg1: .asciiz "Enter the first number: "
msg2: .asciiz "\nEnter the second number: "
bitsStore: .asciiz "\nbits to store the number"
bitsNotStore: .asciiz "\ncannot count bit in "
result: .asciiz "\nThe result of addition is: "
comp: .word 534

.text
li $v0,4
la $a0,msg1 // taking input for count
syscall

li $v0,5
syscall
move $t1,$v0 // reading the count and loading to $t1

li $v0,4
la $a0,msg2 // taking input for num
syscall

li $v0,5
syscall
move $t2,$v0 // reading the count and loading to $t1

Add $t3,$t1,$t2 // adding count and num and storing result in $t3
lw $t0, comp // load comp value declared in .data section in $t0

bgt $t3, $t0, numberGreater // if (count+num > comp)
blt $t3, $t0, numberSmaller // if(count+num < comp)

numberGreater:
li $v0, 4
la $a0, bitsStore //printing msg
syscall

li $v0,1
move $a0,$t1 // according to question there is no need to write bit_count function
syscall // so just print the value of count

b exitLabel

numberSmaller
li $v0, 4
la $a0, bitsNotStore   //printing msg
syscall

li $v0, 1
move $a0,$t3 // print (count+ num) value
syscall

b exitLabel

exitLabel:

li $vo, 10
syscall

I above code i had take the input from user, according to the ques the values are pre defined as 149, 432, if you want to do so here is the code below:

.data

comp: .word 534

count: .word 149

num: .word 432

.text

w $t1, count

w $t2, num

lw $t0, comp

// and continue the above program code from add command onward...

There can be other ways to solve the question, if you have doubts then ask...

Add a comment
Know the answer?
Add Answer to:
Implement the functionality of "main" function for the following C code in MIPS assembly. Comment your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4,...

    MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4, 5, 6); int main) int num, position; scanf("%d",&num) ; position search(array, printf("The position is: num, 5); %d\n",positio int search(int array, int num, int size int position =-1; for(int i-0;i<size; i++) if(array [i]=num) { position-i; break; return position; Register map $s1: position $a0: array address $a1: num . $a2: size . $VO: return value

  • MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i...

    MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls...

    Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls multiply. You are also given the argument registers to be used. /* C-program */ int multiply (int number, int times) { int f; f = number * times return f; } # MIPS PROGRAM # assumes we have called the program leaf_example # $a0=number, $a1=times, $s0=f main: $addi $a0,$zero,3 $addi $a1,$zero,2 jal multiply # place the address into $ra j EXIT2 EXIT2: j OS...

  • 1. [2 points] Write a MIPS assembly language program of the following C function and the...

    1. [2 points] Write a MIPS assembly language program of the following C function and the code to call the function: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; مهه Arguments g, h, i, and j are passed to the function in registers $a0, $al, Şa2, and $a3, respectively while f in $50 (hence, need to save $50 on stack), and the result is to be stored...

  • For the following C statement, what is the corresponding MIPS assembly code?

    For the following C statement, what is the corresponding MIPS assembly code?  Assume that the variables i, and j are assigned to registers $s0, $s1, respectively. Assume that the base address of the arrays A and B are in registers $s2 and $s3, respectively. B[i] = A[i] - 10

  • For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables...

    For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, h, and i are given and could be considered 32-bit integers as declared in a C program. Use a minimal number of MIPS assembly instructions: f g(h 5); For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, h, and i are given and could be considered 32-bit integers as declared in a C...

  • In mips code please Implement the following C code in MAL and develop a small main...

    In mips code please Implement the following C code in MAL and develop a small main program that tests your implementation on an integer array. You must use nested procedures in your implementation. int getArrAvg (int Arr) int sum0 for (int i = 0; i < 10; i += 1) sum-addfn (sum, Arr[i]) return sum/10; int addfn (int a, int b) return ab;

  • Convert the following code in to ARM assembly language. Triple Max // Return max of three...

    Convert the following code in to ARM assembly language. Triple Max // Return max of three variables int max(int a, int b, int c) { // Fill in your own code for this function } int main() { // Use registers for local variables int x = 10; int y = 5; int z = 20; int max = max(x, y, z); printf("max = %d\n", max); return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT