0% found this document useful (0 votes)
7 views26 pages

IT3030E CA Chap3 ISA Exercises

Uploaded by

Thịnh Đặng
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)
7 views26 pages

IT3030E CA Chap3 ISA Exercises

Uploaded by

Thịnh Đặng
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/ 26

Exercise

Chapter 3. Instruction Set Architecture

IT3283E, Fall 2024 1


RARS 1.6 Introduction

IT3283E, Fall 2024 2


RARS 1.6 Introduction

IT3283E, Fall 2024 3


Hello World 1
.text #Code segment to store instructions
addi t1, zero, 2 # t1 = 2
addi t2, zero, 3 # t2 = 3
add t0, t1, t2 # t0 = t1 + t2

Code segment address: 0x00400000

IT3283E, Fall 2024 4


Directives
❑ Instruct assembler to allocate program objects.
❑ Program structure
.text: store objects in the code segment (instructions)
.data: store objects in data segment (static variables)

❑ Variable declaration (allocation)


.byte/.half/.word: store listed values as bytes/halfs/words
.ascii/.asciz: string and null-terminated string
.space: reserved specified number of bytes

❑ List of directives: see RARS 1.6 Help

IT3283E, Fall 2024 5


Hello World 2

.data #Data segment to store variables


X: .word 100
Y: .word 200
.text #Code segment to store instructions
la t0, X
lw t1, 0(t0) #t1 <- X
la t0, Y
lw t2, 0(t0) #t2 <- Y
add t0, t1, t2 #t0 <- X + Y

IT3283E, Fall 2024 6


Hello World 1

Code segment address: 0x00400000

X and Y
Data segment address: 0x10010000

IT3283E, Fall 2024 7


Exercise
❑ Draw the map of this data segment
.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
IT3283E, Fall 2024 8
❑ Data segment map

IT3283E, Fall 2024 9


Exercise 1
❑ 1.1: Write the RISC-V assembly code equivalent to the
following C statement.
f = g + (h − 5);
❑ Assume that the variables f, g, and h, have already been
placed/allocated in registers x5, x6, and x7 respectively.
❑ Note: a minimal number of RISC-V instructions should be
used.
❑ 1.2: Write a complete assembly program that
l Declares integer variables f, g, h.
l Initialize values for f, g, h.
l Calculate and do the assignment: f = g + (h – 5)

IT3283E, Fall 2024 10


.data
f:.word 0
g:.word 100
h:.word 300
.text
la x8, h
lw x7, 0(x8) #load h to x7
la x8, g
lw x6, 0(x8) #load g to x6
addi x7, x7, -5
add x5, x6, x7 #calculate
la x8, f
sw x5, 0(x8) #store to f

IT3283E, Fall 2024 11


Exercise 2
❑ 2.1 Write the RISC-V assembly code equivalent to the
below C statement.
B[8] = A[i-j];
❑ Assume that the variables i, and j are allocated in
registers x28, and x29 respectively. A and B are two
integer array, with the base address of A and B are in
registers x10 and x11 respectively.
❑ 2.2 Write the complete program in RARS 1.6 that
l Declares two integer arrays A and B with at least 20 elements
for each array.
l Do the operation B[8] = A[i-j]; with i and j in x28 and x29.

IT3283E, Fall 2024 12


.data
A:.space 100 #25x4-byte integers each
B:.space 100

.text
la a0, A
la a1, B
li t0, 100
sw t0, 0(a0) #initialize A[0]=100
sub t3, t3, t4 #i-j, now i and j are both 0
slli t4, t3, 2 #offset of A[i-j]
add a0, a0, t4 #a0 hold addr A[i-j]
lw a0, 0(a0) #load A[i-j]
sw a0, 32(a1) #store to B[8]

IT3283E, Fall 2024 13


Exercise 3
❑ Reverse-compile the following RISC-V assembly code to
the equivalent C code. Assume that the variables f, g, h,
i, and j are allocated at registers x5, x6, x7, x28, and x29,
respectively, and the base address of the arrays A and B
are in registers x10 and x11.

slli x30, x5, 2


add x30, x10, x30
slli x31, x6, 2
add x31, x11, x31
lw x5, 0(x30)
addi x12, x30, 8
lw x30, 0(x12)
add x30, x30, x5
lw x30, 0(x31)

IT3283E, Fall 2024 14


Exercise 7
❑ Find the format and the machine code of the following
instruction:
sw x5, 32(x30)

IT3283E, Fall 2024 15


RISC-V system services (syscall)
❑ Basic input/output services provided by RISC-V system.
l Print string, print character, print integer/floating point to output
console.
l Read string, char, int, float/double from input console.
l Other function: random, dialog,…

❑ Usage:
l Step 1. Load the service number in register a7.
l Step 2. Load argument values, if any, in a0, a1, a2, a3, fa0, ... as
specified.
l Step 3. Issue the ECALL instruction.
l Step 4. Retrieve return values, if any, from result registers as
specified

❑ List of system services: see RARS 1.6 Help

IT3283E, Fall 2024 16


System service: print integer
❑ Input params:
l a7 = 1
l a0 = [integer value to print].

❑ Example:
li a7, 1
li a0, 0x307
ecall

❑ Result:

IT3283E, Fall 2024 17


System service: print string
❑ Input params:
l a7 = 4
l a0 = address of the string to print

❑ Example:
.data
message: .asciz "Khoa \nKy thuat May tinh"
.text
li a7, 4
la a0, message
ecall

❑ Result

IT3283E, Fall 2024 18


System service: read integer
❑ Input params
l a7 = 5

❑ Return
l a0 = integer value read

❑ Example:
li a7, 5
ecall

❑ Result: try run the above code on RARS 1.6


l The ecall instruction activates the input caret at the in/out
console waiting for user input.
l User types in an integer and press Enter → the integer is loaded
to a0.

IT3283E, Fall 2024 19


Hello World 3
❑ Write a program
- Print “Hello!” to console
- Wait for user to press any key
- Print “Have a good day!”

IT3283E, Fall 2024 20


#simple syscall example for input/output
.data
#this is where we declare variables
hello: .asciz "Hello!\n"
goodday: .asciz "Have a good day!\n"
.text
main:
#this is where our code goes
#print hello string
li a7, 4
la a0, hello
ecall
#wait for user to press on keyboard
li a7, 12
ecall
#then say good day
li a7, 4
la a0, goodday
ecall
endmain:
IT3283E, Fall 2024 21
Exercise
❑ Write a program to:
l Input an integer X from console
l Output to screen the appropriate string:
- “odd” if X is an odd number
- “even” if X is an even number

IT3283E, Fall 2024 22


Exercise
❑ 1. Write a subprogram/function to:
l Get an integer X as input argument
l Output the absolute value of X

❑ 2. 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.
❑ 3. Modify the above program so that it repeats the above
process continuously until it reads the value 0 from input
console.

IT3283E, Fall 2024 23


.text
main:
li a7, 5 # read an int to a0
ecall
beq a0, zero, exit # exit if input = 0
jal abs # call abs procedure
li a7, 1 # print return value
ecall
j main
exit: li a7, 10 # terminate, WHY is this necessary?
ecall # see system service 10
end_main:

abs: bge a0, zero, done # if a0>=0 done


sub a0, zero, a0 # else negate a0
done:
jr ra
IT3283E, Fall 2024 24
Exercise
❑ Write a program to read a positive value n from console,
then calculate and print the sum of all numbers from 1 to
n.
❑ Implement the function of calculate sum as a procedure
with below parameters:
l a7: n (upper boundary)
l a0: return value of (1+2+..+n)

IT3283E, Fall 2024 25


Exercise
❑ Develop a simple implementation of the strlen() function
size_t strlen(const char *str)
❑ Write a program to input a string (max of 255 chars),
then print out its length

IT3283E, Fall 2024 26

You might also like