Lecture 5
Pseudo instructions, macros, and includes. Conditions, loops, loads, and stores.
Lecture
Outline
- Program structure: segments and labels.
- Branch instructions and their use in conditions and loops.
- Data segment and data directives (
.byte
,.half
,.word
,.space
,.align
,.ascii
,.asciz
). - Load and store instructions and their use for accessing static data.
- Macros (directives
.macro
,.end_macro
,.include
, and.eqv
) - Examples of programs processing static variables stored in the data segment.
Examples:
- hello.s
- if_then_else.s
- while.s
- for.s
- for_nested.s
- macros1.s
- macros2.s
- macros3.s
- macrolib.s
- fibonacci.s
- euclid.s
- data.s
- loadstore.s
- loadstore2.s
- loadstore3.s
- min_max.s
Workshop
Outline
- Study and run all the examples from the lecture.
- Practice writing programs with conditions and loops.
- Practice writing programs that access values in the data segment.
- Practice using macros. Pay attention to the code generated from macros.
Tasks
-
Write a program that inputs two integer values
x
andy
and prints all the values in the rangemin(x, y)..max(x, y)
. -
Write a program that fills an array of 32 integers from values from the standard input. It reads values in a loop and finishes reading when all 32 values are read or when value
0
is read. -
Write a program that inputs two positive integer values
N
andD
, finds their quatient (Q
) and remainder (R
) using the algorithm below, prints the result.function divide_unsigned(N, D) Q := 0; R := N while R ≥ D do Q := Q + 1 R := R − D end return (Q, R) end
-
Write your own macros
print_hex
andprint_bin
for printing values in hexadecimal and binary formats respectively. What if you want to print immediate values? What kind of macro do you need in this case? -
Write a program that inputs two unsigned integer values
x
andy
, calculatesx ** y
(x
raised to the power ofy
), and prints the result. The exponentiation should be implemented as a multiplication in a loop. If an overflow occurs, the program must exit the loop and print an error message.
Homework
Solve the following tasks:
Commit the programs to your private GitHub account. Place them into the folder ca/lab05
.
References
- RARS help system (click in the main menu
Help > Help
or pressF1
). - Multiplication and division algorithms. Sections 3.3 and 3.4 in [CODR] and Appendix J in [CAQA].
- Macros (Wikipedia).
- Macros in Assembly Language (Wikipedia).
- RISC-V Assembly Programmer’s Manual.
- Memory Alignment (Wikipedia).