0% found this document useful (0 votes)
9 views

Chapter 3_ISA_Assignment

Uploaded by

Ngô Linh Chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chapter 3_ISA_Assignment

Uploaded by

Ngô Linh Chi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

Assignment Chapter 3: ISA


Student ID 20235984
Student Name Ngo Minh Ngoc

ASSIGMENT 1:

1. My code

# Ngo Minh Ngoc 20235984

.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

# Ngo Minh Ngoc 20235984

addi x7, x7, -5 #h = h-5


add x5,x6,x7

(1.2 in Exercise 1)

Write a complete assembly program that:

-Declares integer varibales f, h, h

- Initialize values for f, g, h

- Calculate and do the assignment f = g + (h-5)

1. My code

# Ngo Minh Ngoc 20235984

.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:

- We choose f = 20, g = 30, h= 40  f = 30 + (40-5) = 65  which is correctly stored in the “0x10010000”


memory address (“0x00000041” in hexadecimal = 65 in decimal)
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

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

#Ngo Minh Ngoc 20235984


.text
sub t3, t3, t4 # t3 = t3 - t4 (index i)
slli t4, t3, 2 # t4 = 4*t3 (number of bytes to jump)
add a0, a0, t4 # a0 stores address of A[i-j] = adress of A[0] +t4
lw a0, 0(a0) # a0 stores A[i-j]
sw a0, 32(a1) # as B[8] means we have to jump 32 bytes from A[0]

(2.2 in Exercise 2)

Write the complete program in RARS that :

- 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

#Ngo Minh Ngoc 20235984


.data
A: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
B: .word -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18,
-19, -20
.text
li x28, 8 # choose i = 8
li x29, 3 # choose j = 3

la a0, A # a0 stores address of A[0]


la a1, B # a1 stores address of B[0]
sub t3, t3, t4 # t3 = t3 - t4 (index i)
slli t4, t3, 2 # t4 = 4*t3 (number of bytes to jump)
add a0, a0, t4 # a0 stores address of A[i-j] = adress of A[0] +t4
lw a0, 0(a0) # a0 stores A[i-j]
sw a0, 32(a1) # as B[8] means we have to jump 32 bytes from A[0]
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

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:

1) Code conversion and Explaination:

1. slli x30, x5, 2  x30 = f * 4;


( shifts the value in register x5 (which corresponds to variable f) to the left by 2 bits. In C, this is
equivalent to multiplying f by 4 (since shifting left by 2 bits is the same as multiplying by 22=42^2 =
422=4). The result is stored in register x30)
2. add x30, x10, x30  x30 = (int)&A[f]; // Address of A[f]
( the value in register x10 (base address of array A) is added to x30. This effectively calculates the address
A[f] since multiplying f by 4 gives the byte offset for an element of size 4 bytes)

3. slli x31, x6, 2  x31 = g * 4;


(shifts the value in register x6 (which corresponds to variable g) to the left by 2 bits. In C, this is
equivalent to multiplying g by 4)
4. add x31, x11, x31  x31 = (int)&B[g]; // Address of B[g]
(The base address of array B (x11) is added to x31. This calculates the address B[g])

5. lw x5, 0(x30)  f = A[f]; // Load the value of A[f] into f


(Load the value from the memory address in x30 into register x5. This means x5 now holds the value of
A[f])

6. addi x12, x30, 8  x12 = (int)&A[f + 2];


(This instruction adds 8 to the value in x30, storing the result in x12. Essentially, x12 now holds the
address A[f + 2])
7. lw x30, 0(x12)  x30 = A[f + 2];
(Load the value from the memory address stored in x12 into register x30. So, x30 now holds the value A[f
+ 2] )

8. add x30, x30, x5  x30 = A[f + 2] + f;


(Add the values in registers x30 and x5, storing the result in x30. This adds A[f + 2] to f)
9. lw x30, 0(x31)  x30 = B[g];
(Load the value from the memory address stored in x31 into register x30. So, x30 now holds the value
B[g])

2) The equivalent C code:

i = f // i and f are indexes


f = A[f]; // Change f: Load A[f] into f
int temp1 = A[i + 2]; // Load A[i + 2] (i=f before f change to A[f])
int temp2 = temp1 + f; // temp2 = A[i+2] + A[i]
int result = B[g]; // Load B[g]
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

ASSIGMENT 5:

Find the format and the machine code of the following instruction: “sw x5,32(x30)”

1. Format and machine code:

2. My code

#Ngo Minh Ngoc 20235984


.text
li x5, 7
sw x5, 32(x30)

3. Verification:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

ASSIGMENT 6:

Write a program to:

- Input an iteger X from console


- Output to screen the appropriate string:
 “odd” if X is an odd number
 “even” if X is an even number

3. My code

# Ngo Minh Ngoc 20235984 -Bai 8


.data
string0: .asciz "even"
string1: .asciz "odd"

.text

main:
li a7, 5 # Input the desired number
ecall

li t0, 1
and t1, t0, a0

beq t1, zero, even

# print out the message if odd


li a7, 4
la a0, string1
ecall
j end
# print out the message if even
even:
li a7, 4
la a0, string0
ecall

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:

a) Write a sub program/function to:


- Get an integer X as input argument
- Output the absolute value of X
1. My code (the function)

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

# Ngo Minh Ngoc 20235984 -Bai 9.1.2.3

.text

main:
li a7, 5 # Input the desired number
ecall

jal abs # jump and link to the abs function


# print the abs
li a7, 1
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

# Ngo Minh Ngoc 20235984 -Bai 9.1.2.3

.text

main:
li a7, 5 # Input the desired number
ecall
beq a0, zero, exit # if a0 = 0 exit

jal abs # jump and link to the abs function


# print the abs
li a7, 1
ecall
# print the endline character
li a7, 11
li a0, 10
ecall

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

# Ngo Minh Ngoc 20235984


.text
li a7, 5 #input n
ecall

li t0, 1 #Initialize the number i = 1


li s0, 0 #initialize sum = 0

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:

li a7, 1 # output sum


addi a0, s0, 0 # a0 = sum
ecall

2. Ouput result:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

- Implement the function of calculate sum as a procedure with below parameters:


- A7: n (upper boundery)
- A0: return value of (1+2+…+n)

3. The function (Not implemented in the whole program with main function yet)

sum_procedure:

li t0, 1 #Initialize the number i = 1


li a0, 0 #initialize sum = 0

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

The whole program with the implementation of the function:

# Ngo Minh Ngoc 20235984

.text
main:
li a7, 5 # input the number n
ecall

add a7, a0, zero # a7 = n (for the function parameter


jal sum_procedure

# print the sum


li a7, 1
ecall
exit:
li a7, 10
ecall
end_main:
Computer Architecture – Chapter 3 Assignment: ISA – Ngo Minh Ngoc 20235984

sum_procedure:

# li a7, 5 #input n
# ecall

li t0, 1 #Initialize the number i = 1


li a0, 0 #initialize sum = 0

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:

You might also like