Lab8-RISC Input Output
Lab8-RISC Input Output
This lab exercise uses the Logisim file mips-lab.cir provided in Lab-7. You can use a machine
code assembler tool like Mars (download link) to quickly compile the machine code. In the MIPS
circuit, the Southbridge is configured with certain special addresses instead of regular memory
addresses. These addresses are mapped to interact with I/O devices that we explored in the previous
lab. For example, when we write data to a special memory area at address 0x80000008, the data
is not written to memory but will be output to the TTY Console. Any address below 0x80000000
is a normal memory address, while addresses above 0x80000000 are special I/O addresses, as
revealed through the logic of the northbridge and southbridge. The special I/O addresses are as
follows:
0x80000000: Keyboard status (1 bit, read-only)
0x80000004: Keyboard data (8 bit, read-only)
0x80000008: Console output (8 bit, write-only)
For example: if register r10 contains the value 0x41, which is the ASCII code for the character
‘A’, the following MIPS instructions can be used to output the character ‘A’ to the console
To read the status of the keyboard in MIPS, you can use the memory map I/O address
0x8000000, which hold the keyboard status (1 bit, readonly).
To continously check the keyboard status and determine if a key has been pressed, you can use a
loop to repeatedly check the keyboard status. If a key is pressed, the program will proceed to
read the key value.
1
Computer Hardware Lab
This program will read a line of text from the keyboard and store it in RAM data memory
1. printString(p)
2
Computer Hardware Lab
Write a function to output a string to the console, which takes a parameter p (stored in register
$4), where the value of this register is the address of the string to be printed in memory. Your
program will send this string to the console (excluding the null terminator), and the function will
be named printString. Note that the program should always use the jr $31 instruction to return at
the end of the function.
initialize some register to zero, to keep count
repeat forever {
load a byte from the next position of the string
if that byte was a zero, then stop looping and don't print this
character
otherwise:
write (store) that byte to the console, using the special address
and advance $4 to the next memory location
and also increment the count
}
put a count of how many character were printed into register r2
return
Test program.
main:
addiu $4, $0, 0x30 # argument is memory location 0x30
jal readWithoutEcho # jumps to the readWithoutEcho code
nop # ignore return value in r2, we don't need it
nop
addiu $4, $0, 0x30 # argument is memory location 0x30
jal printString # jumps to your printString code
nop
nop
addiu $3, $0, 4 # put constant 4 into register
bne $2, $3, main # go back and do it again if string wasn't 4 letters
long
endlessloop:
j endlessloop # if user typed 4 letter word, then loop forever
endlessly
Check the result in Logisim. Save the entire program with the name readwithoutecho.txt
2. Create a function named readString tha performs a similar function to readWithoutEcho with
two changes:
- The program will stop when a space character or a newline character is enterd.
Create a test program to ensure the functionality of the function you created.
3. Write a function atoi, which takes a parameter p, the address of string stored in memory, and
returns an integer i. The returned value is stored in register $2. This function can handle both
positive and negative numbers with the input string assumed to be a valid string.
3
Computer Hardware Lab
.data
secret: .word -357
msg: .asciz "Please type a number: "
toolow: .asciz "Too low, try again!\n"
toohigh: .asciz "Too high, try again!\n"
hurray: .asciz "Nice guess! "
# note: the above should all fit in data memory below address 0x100
.text
main:
la $4, msg # put address of message into argument register
jal printString # call printString("Please type a number: ")
nop
addiu $4, $0, 0x100 # use location 0x100 for argument
jal readString # call readString(0x100)
nop
addiu $4, $0, 0x100 # use location 0x100 for argument
jal atoi # call atoi(0x100)
nop
la $3, secret
lw $3, 0($3)
sub $3, $2, $3
beq $3, $0, justright
bltz $3, over
under:
la $4, toolow
jal printString
nop
j main
over:
la $4, toohigh
jal printString
nop
j main
justright:
la $4, hurray
jal printString
nop
j justright
4. There are 10 LEDs in the circuit. You need to write a function named led(n), which takes a
parameter n and toggles the state of the corresponding LED. Note that you should write to the
special memory address 0x8000000C to control the LEDs. Write a test program for the above
function by controlling the LEDs through input parameters from the console. Save the file with
the name led.txt
5. Write a program to implement the Caesar Cipher. You can refer to how Caesar Cipher works
online. Your program can use the functions you have already implemented in the previous
4
Computer Hardware Lab
tasks. The program should first ask the user to input a number and convert this number into an
integer value n (you can use the readString and atoi functions). Then, prompt the user to enter
a secret string and store it in memory at address p (use readWithoutEcho to prevent displaying
the secret string on the console). Next, the program will perform the encryption using the
Caesar Cipher method, shifting each character by n positions. For example, if the character is
'a' and n=3, the resulting character will be 'd'. Save the program with the name ceasar.txt.
Please type a number: (user enters 17, and this is shown on screen)
Please type a secret sentence: (user types "Hello world!", this doesn't appear
on screen)
message is: Yvccf nficu!