Question

In: Computer Science

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 0xA000000000000000 and 0x2000000000000000, respectively.

(1) What is the value of x30 for the following assembly code? add x30, x5, x6

(2) For the contents of registers x5 and x6 as specified above, what is the value of x30 for the following assembly code? sub x30, x5, x6

(3) For the contents of registers x5 and x6 as specified above, what is the value of x30 for the following assembly code? add x30, x5, x6 add x30, x30, x6

Solutions

Expert Solution

1.

We can write the required C expression in two steps as :

a = b + c;

a = a + (-2);

Converting the individual broken-up C-statements, we get the followign sequence of assembly-instructions :

add x10, x11, x12

addi x10, x10, -2

2.

The given two assembly instructions can be individually converted as :

add e, f, g

=> e = f + g;

add e, h, e

=> e = h + e;

Expanding the second result, we get the single C-statement as :

=> e = h + e

=> e = h + (f + g);

=> e = f + g + h;

3.

(1)

add x30, x5, x6

=> x30 = x5 + x6

=> x30 = 0xA000000000000000 + 0x2000000000000000

=> x30 = 0xC000000000000000

(2)

sub x30, x5, x6

=> x30 = x5 - x6

=> x30 = 0xA000000000000000 - 0x2000000000000000

=> x30 = 0x8000000000000000

(3)

add x30, x5, x6

=> x30 = x5 + x6

=> x30 = 0xA000000000000000 + 0x2000000000000000

=> x30 = 0xC000000000000000

add x30, x30, x6

=> x30 = x30 + x6

=> x30 = 0xC000000000000000 + 0x2000000000000000

=> x30 = 0xE000000000000000


Related Solutions

Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main()...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main() { int i = 2, j = 2 + i, k; k = i * j; printf("%d\n", k + j); return 0; } 2) #include <stdio.h> int main() { int i = 2, j = 2 + i, k = j / 2; if (k == 1) { printf("%d\n", j) k = k * 2; if ( k == j) { printf("%d\n|, j); }...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in register x5 and that the base address of array A that holds doubleword integers is in x20. addi x5, x0, 0 addi x6, x0, 50 addi x28, x20, 0 loop: bge x5, x6, end ld x7, 0(x28) bge x7, x0, next sub x7, x0, x7 sd x7, 0(x28) next: addi x5, x5, 1 addi x28, x28, 8 jal x0, loop end: Can you rewrite...
Convert the following C program into the RISC-V assembly code. You should look up a table...
Convert the following C program into the RISC-V assembly code. You should look up a table to convert the lines manually and you can use "ecall" when appropriate (Don't make it too complicated than it needs to be): #include int main() { int x = 30, y = 17; printf("x * y = "); printf("%d\n", x*y); return 0; }
3 – Write the following sequence of code into the RISC-V assembler. Assume that x, y,...
3 – Write the following sequence of code into the RISC-V assembler. Assume that x, y, and z are stored in registers x18, x19, and x20 respectively. z = x - 2; x = z +4 - y;
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
Write assembly code for the following machine code. Assume that the segment is placed starting at...
Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
a. Write machine code for the following assembly code. Assume that the segment is placed starting...
a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
Assembly code Write an assembly program that converts all uppercase letters to their corresponding lower cases....
Assembly code Write an assembly program that converts all uppercase letters to their corresponding lower cases. meanwhile all characters that are not upper case letters should remain unchanged. Hint: ASCII: a = 97, z = 122, A = 65, Z = 90 String : "Riders On The Storm - The Doors"
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT