Chapter 3_ISA_Assignment
Chapter 3_ISA_Assignment
ASSIGMENT 1:
1. My code
.data
arr16: .space 16
arr: .byte 'a', 'b','c'
st1: .asciz "abcd"
X1: .word 0xFFFFFFFF
st2: .ascii "efgh"
X2: .word 0xFFFFFFFF
st3: .asciz "1234"
X3: .word 0xFFFFFFFF
st4: .asciz "5678"
X4: .word 0xFFFFFFFF
2. Verification:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
ASSIGMENT 2:
(1.1 in Exercise 1)
Write the RISCV assembly code euivalent to the following C statement: f = g + (h-5); ( assume that the variables
f, g, h have already been placed in registers x5, x6, x7 repectively
(1.2 in Exercise 1)
1. My code
.data
f: .word 20
g: .word 30
h: .word 40
.text
la x8, h #s0 stores address of h label( address of the value 40)
lw x7, 0(x8) #x7 = 40: load the value 40 (in address stored in x8) to x7
la x8, g #s0 stores address of g label( address of the value 30)
lw x6, 0(x8) #x6 = 30: load the value 30 (in address stored in x8) to x6
addi x7, x7, -5 # x7 = 40 - 5 (h-5)
add x5, x6, x7 # x5 = 30 + (40-5)
la x8, f #s0 stores address of f label( address of the value 420
sw x5, 0(x8) # stores the value just calculated x5 to address of f
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
2) Ouput result:
ASSIGMENT 3:
(2.1 in Exercise 2)
Write the RISCV assembly code equivalent to the below C statement: B[8] = A[i-j], assuming the variables I, j are
located in register x28, x29. A abd B are 2 integer array, with the base address of A and B are in register x10 and
x11 respectively
2. My code
(2.2 in Exercise 2)
- Declare 2 integer arrays A and B with at leaste 20 elements for each array
- Do the operation B[8] = A[i-j]; with I and j in x28 and x29
- My code
3) Ouput result:
- We choose i = 8, j = 3, therefore i – j = 5, and A[3] = 6, which is stored in a0 ( we can see it in the register
tab). Then the program load the value A[i-j] (which is now stored in a0) to B[8] ( meaning stores in the
memory 32 bytes away from the adress of B[0] – stored in a1)
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
ASSIGMENT 4:
ASSIGMENT 5:
Find the format and the machine code of the following instruction: “sw x5,32(x30)”
2. My code
3. Verification:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
ASSIGMENT 6:
3. My code
.text
main:
li a7, 5 # Input the desired number
ecall
li t0, 1
and t1, t0, a0
end:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
4) Ouput result:
- Odd case:
- Even case:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
ASSIGMENT 7:
abs:
# The integer X as input argument is stored in a0
bge a0, zero, finish # if the number is already positive don’t need to change sign
sub a0, zero, a0 # else change sign of the number
finish:
jr ra #= # jump and return to the instruction behind jal
b) Use the above function to write a program that reads an input integer from input console, then calculate and
display the absolute value of that integer
2. My code
.text
main:
li a7, 5 # Input the desired number
ecall
exit:
li a7, 10
ecall
end_main:
abs:
bge a0, zero, finish # if the number is already positive don’t need to change sign
sub a0, zero, a0 # else change sign of the number
finish:
jr ra #= # jump and return to the instruction behind jal
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
5) Ouput result:
c) Modify the above program so that it repeats the above process continuously until it reads the calue 0 from
input console
3. My code
.text
main:
li a7, 5 # Input the desired number
ecall
beq a0, zero, exit # if a0 = 0 exit
j main
exit:
li a7, 10
ecall
end_main:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
abs:
bge a0, zero, finish
sub a0, zero, a0
finish:
jr ra #= # jump and return to the instruction behind jal
6) Ouput result:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
ASSIGMENT 8:
- Write a program to read a positive value n from console, then calculate and print the sum of all numbers
from 1 to n
1. My code
loop:
add s0, s0, t0 #Sum += i
beq t0, a0, end_sum #If(i == n) --> Stop summing
addi t0, t0, 1 # i++
j loop
end_sum:
2. Ouput result:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
3. The function (Not implemented in the whole program with main function yet)
sum_procedure:
loop:
add a0, a0, t0 #Sum += i
beq t0, a7, end_sum #If(i == n) --> Stop summing
addi t0, t0, 1 # i++
j loop
end_sum:
finish:
jr ra # jump and return to the instruction behind jal
.text
main:
li a7, 5 # input the number n
ecall
sum_procedure:
# li a7, 5 #input n
# ecall
loop:
add a0, a0, t0 #Sum += i
beq t0, a7, end_sum #If(i == n) --> Stop summing
addi t0, t0, 1 # i++
j loop
end_sum:
finish:
jr ra #= # jump and return to the instruction behind jal
2) Ouput result:
- Here we can see the program with the implementation of the Sum function print out the right
answer( input 5 output 15)
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
ASSIGMENT 9:
- Develop a simple implementation of the strlen() function, write a program to input a tring (max of 255
chars) then print out its length
1. My code
2. # Ngo Minh Ngoc 20235984
3. .data
4. string: .space 255
5. message1: .asciz "Nhap xau: "
6. message2: .asciz "Do dai xau la: "
7.
8. .text
9.
10. main:
11. getstring:
12. # Print out "Nhap xau: "
13. li a7, 54
14. la a0, message1
15. la a1, string
16. li a2, 255
17. ecall
18.
19. get_length:
20. li t0,0 # set t0 stores i = count = 0 --> both to count character and to
index through the string
21. la a0, string # a0 stores the address of the input string
22.
23. add s0, a0, zero # Set s0 stores the address of string (string[0]), (since the
address is previously stored in a0 after "ecall"
24. check_char:
25. add t1, s0, t0 # t1 holds the address of string[i], t1 = address of string[0]
+ i
26. lb t2, 0(t1) # t2 holds the chacracter string[i] (whose address is stored in
t1)
27. beq t2, zero, end_of_str # branch to end the loop if NULL char is detected
28. # else
29. addi t0, t0, 1 # increment i, i++
30. j check_char # repeat the loop
31. end_of_str:
32. end_of_get_length:
33. print_length:
34. # Print out "Do dai xau la: "
35. li a7, 4
36. la a0, message2
37. ecall
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984
38.
39. addi t0, t0, -1
40.
41. # Print out the number of characters
42. li a7, 1
43. add a0, t0, zero
44. Ecall
2. Ouput result: