Question

In: Computer Science

Write assembly code for the following C procedure: i = 1; sum = 0; while (i...

Write assembly code for the following C procedure:

i = 1; sum = 0;

while (i ≤ n) {

sum += i;

++i;

}

i and sum corresponds to $s1 and $s2. n is preloaded into $s3

Solutions

Expert Solution

Hope this will help you. If you have any doubt please let me know.

Notes: You specified only Assembly language based on $s1 and $2 and $s3 register I am assuming you want it in Mips code. I have made 2 versions of this program. In 1st version n’s value is taken from user while in case of 2nd version n’s value is predefined. Both are working perfectly fine in Mars tool.

Please let me know if you want any modification in this program.

Commenting is also done with in code.

Please feel free to ask me if you need anything else or you have any doubt.

Please also see the screenshot (for both code 2nd version is simplest)

----------------------------------------------------------------Version1----------------

.text  
   .globl   main  
main:
   # Print string msg1
   li   $v0,4       # printing string syscall code = 4
   la   $a0, msg1   # load the address of msg1
   syscall
  
   # read a input from use
   li   $v0,5       # to read int we need syscall code = 5
   syscall  
   move   $s3,$v0       # syscall results returned in $v0 storing user value into $s3 user input value in $s3
  
   addi $s1,$zero,1 #i's vale in $s1=1
   addi $s2,$zero,0 # sum's value in $s2=0
   loop:
   slt $s4,$s1,$s3 # if i's value is less than n 's value, $s4's value is set to 1 otherwise it is zero
   beq $s4,$0,end #if both are equal jump to end block
   add $s2,$s2,$s1 # adding i's value in sum
   addi $s1,$s1,1 #incrementing i
   j loop
  
   end:
   add $s2,$s2,$s1 # adding a last value of i into sum
  
   # Print string msg2
   li   $v0, 4 #printing sting syscall =4
   la   $a0, msg2 #loading the address of msg2
   syscall  
   # Print output in decimal
   li   $v0,1       # printing a dec syscall code = 1
   move   $a0, $s2  
   syscall
  
   li   $v0,10       # exit
   syscall

   .data
msg1:   .asciiz   "Enter Number that you to store in $s3 (or you want to sum upto that number): "
msg2:   .asciiz   "the sum is= "

---------------------------------------------Version2-----------------------------

   .text  
   .globl   main
  
main:
   add $s3,$zero,5 # $s3 value set to 5 ,you can change value directy from here
   addi $s1,$zero,1 # i's vale in $s1=1
   addi $s2,$zero,0 # sum's value in $s2=0
   loop:
   slt $s4,$s1,$s3 # if i's value is less than n 's value, $s4's value is set to 1 otherwise it is zero
   beq $s4,$0,end #if both are equal jump to end block
   add $s2,$s2,$s1 # adding i's value in sum
   addi $s1,$s1,1 #incrementing i
   j loop
  
   end:
   add $s2,$s2,$s1 # adding a last value of i into sum
  
   li   $v0,1       # printing a dec syscall code = 1
   move   $a0, $s2  
   syscall
  
   li   $v0,10       # exit
   syscall
------------------------------------- screenshot-----------------------


Related Solutions

Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
1. For the following C statement, write the corresponding RISC-V assembly code. Assume that the C...
1. For the following C statement, write the corresponding RISC-V assembly code. Assume that the C variables a, b, and c, have already been placed in registers x10, x11, and x12 respectively. Use a minimal number of RISC-V assembly instructions. a = b + (c − 2); 2. Write a single C statement that corresponds to the two RISC-V assembly instructions below. add e, f, g add e, h, e 3. Assume that registers x5 and x6 hold the values...
MIPS assembly code: procedure:                   addi $s0,$zero,0 loop:                   slt
MIPS assembly code: procedure:                   addi $s0,$zero,0 loop:                   slti $t1, $s0, 7                   beq $t1, $zero, exit                   addi $s0,$s0,1                   j loop exit:                   add $v0, $zero, $s0                   jr $ra       What is the corresponding high-level programming language code? Write a code in your preferred language. What is the result? (5 pts) Code: int add(int a, int b) {       return a+b; } int sub(int a, int b) {      ...
I need to write a c/c++ code to find the maximum sum in those possible combinations...
I need to write a c/c++ code to find the maximum sum in those possible combinations of arrays. There are N arrays with S elements. ex2: 4 arrays with 3 elements.We want to find the greater number in the same column of two arrays(may possible be three or four...arrays), and sum those greater number to find the greatest sum in all of the combinations in those 4 arrays, like: A: [50, 60, 70] B: [80, 40, 20] C: [30, 100,...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1,...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1, and base address of b[] in $s2, respectively. Note that the element’s data type in array b is integer (word). Please do not change the C code structure and please comment your MIPS code. for (i = 100; i>a; i--) {          b[i] = 16*i; b[i+1] = 16*(i+1); b[i+2] = 2*i + 16; }
Need to code this is assembly language. Using the windows32 framework, write a procedure to read...
Need to code this is assembly language. Using the windows32 framework, write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the output...
Write the following assembly code file into C code. .globl main .text main:    # Tests...
Write the following assembly code file into C code. .globl main .text main:    # Tests simple looping behaviour    li t0, 60    li t1, 0 loop:    addi t1, t1, 5    addi t0, t0, -1    bne t1, t0, loop    bne t1, zero, success failure:    li a0, 0    li a7, 93 # a7 is what determines which system call we are calling and we what to call write (64)     ecall # actually issue...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j; b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I; Explain each step in detail! my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2 2) lw $t0, 0 ($s3)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT