Introduction to Lab #2
José Nelson Amaral
General Intro to 229 Labs
• In 229, a “lab” is a programming assignment:
• A lab requires many more hours of work than the time allocated for lab sessions.
• Labsessionsare“consultinghours”whenTAsareavailabletoanswerquestionsandto help.
• Reading/work prior to the lab date/time is essential.
• The lab assignments will be progressively more difficult, and will require more time as
the term advances.
• A CMPUT 229 lab is not a “lab” in the sense of a chemistry lab.
The calculator Program
Write a RISC-V assembly program that acts as a calculator for reverse Polish
notation/postfix expressions.
Infix notation: Postfix notation:
(1+2)*3=9 12+3* =9
Types of input tokens for calculator
Token Type
non-negative integer
TERMINATION
Operation of the calculator • read a token from the input list
• if token == OPERAND (a non-negative value) • push value into the stack
• if token == PLUS or token == MINUS • pop two topmost values from the stack
• perform operation
• push result into the stack
• if token == TERMINATION
• print out the value that is on top of the stack • terminate the program
How does the stack grow?
TopOfTheStack
0x10001024 5 0x10001020 0x1000101C 0x10001018
Initial State: Stack only contains value 5 Action: Push 1 on top of stack
Action: Push 4 on top of stack
Action: Pop 4 from top of stack Action: Pop 1 from top of stack Action: Push 3 on top of stack
Input Token List:
0x1000100C 0x10001008 0x10001004 0x10001000
Token Type:
0x10001024 0x10001020 0x1000101C 0x10001018
Pop 1 and 5 from stack, execute the operation 5-1
and push result into the stack
TEORPMEIINRNUASTNIDON
Pop 4 from stack and write it to output
Formatting and Style
• Check the provided example.s file • Check the lab grading marksheet
Assembler Syntax
comments begin with a sharp sign (#) and run to the end of the line.
identifiers are alphanumeric sequences, underbars (_), and dots (.) that do not
begin with a number.
labels are identifiers placed at the beginning of a line, and followed by a colon.
.data item:
.globl main
lw s3, item
add t1, s3, s3 add t1, t1, t1 add t1, t1, s6 lw t0, 0(t1) bne t0, s5, Exit add s3, s3, s4 jal zero, Loop
main: Loop:
# t1 <- Addr(save[i])
# t0 <- MEM[save[i]]
# if save[I] != k goto Exit # i <- i + j
# goto Loop
CS Help, Email: tutorcs@163.com
.data .word 1
.text .globl
identifies the beginning of the data segment (in this example this segment contains a single word). stores the decimal number 1 in 32-bits (4 bytes)
identifies the beginning of the text segment (where the instructions of the program are stored).
Assembler Directives
declares the label main global (so that it can be accessed from other files).
.data item:
.globl main
lw s3, item
add t1, s3, s3 add t1, t1, t1 add t1, t1, s6 lw t0, 0(t1) bne t0, s5, Exit add s3, s3, s4 jal zero, Loop
main: Loop:
# t1 <- Addr(save[i])
# t0 <- MEM[save[i]]
# if save[I] != k goto Exit # i <- i + j
# goto Loop
Pseudo Instructions
# What's going on here ?
.word 12, 34, 56, 78, 90
outputMsg:
.asciz "\n Result = "
.asciz "\n\n"
la t0, val xor t1, t1, t1 xor t2, t2, t2
sub t3, a1, t2
blez t3, exit
lw t4, 0(t0)
add t1, t1, t4
add t2, t2, 1
addu t0, t0, 4
jal zero, loop
div t5, $t1, $a1 li a7,4
la a0, outputMsg ecall
add a0, 0, t5 ecall
la a0, newln ecall
li a7, 10
pseudo instruction that loads the immediate value in the register
pseudo instruction that loads the address of specified label into register
OS-style call to obtain services from RARS: a0-a2: arguments
a7: system call code a7: return value
Using GitHub
• While you can either type directly or copy and paste into an editor provided by github, this is not recommended.
• Learn to use basic command-line commands for git such as: • clone
• commit • push
• When you initially clone the repository provided, you will see a Code folder.
• In this folder there will be a calculator.s file. • Your solution goes at the bottom of this file.
• Your code must start under the label ‘calculator’.
浙大学霸代写 加微信 cstutorcs
CMPUT 229 Student Submission License
• Carefully read the text of the CMPUT 229 Student Submission License to understand what you are allowed to do with your code before and after submission.
• After reading the license, complete the following information, in the calculator.s file, to acknowledge that you have read and understood the license:
• Every lab will have a common.s file that performs some actions
and then calls the function written by the student.
• Read carefully and try to understand the common.s file as a way to learn.
程序代写 CS代考 加QQ: 749389476