0% found this document useful (0 votes)
17 views5 pages

Lab8-RISC Input Output

This document outlines a lab exercise focused on RISC programs with input/output using MIPS assembly language. It details the use of special memory addresses for I/O operations, provides examples of reading from the keyboard and outputting to the console, and describes various functions such as reading strings and implementing a Caesar Cipher. Additionally, it includes test programs and instructions for saving the work in specific text files.

Uploaded by

c9hisme0512
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)
17 views5 pages

Lab8-RISC Input Output

This document outlines a lab exercise focused on RISC programs with input/output using MIPS assembly language. It details the use of special memory addresses for I/O operations, provides examples of reading from the keyboard and outputting to the console, and describes various functions such as reading strings and implementing a Caesar Cipher. Additionally, it includes test programs and instructions for saving the work in specific text files.

Uploaded by

c9hisme0512
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/ 5

Computer Hardware Lab

Lab 8: RISC PRORAMS WITH 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

LUI r8, 0x8000 # sets r8 = 0x80000000


ORI r8, r8, 8 # sets r8 = 0x80000008 (address of console output)
SB r10, 0(r8) # sends character from r10 to the console output

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

LUI r8, 0x8000 # sets r8 = 0x80000000 (address of keyboard status register)


LB r9, 0(r8) # reads the status from the keyboard, puts it in r9.

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.

LUI r8, 0x8000 # sets r8 = 0x80000000


ORI r8, r8, 4 # sets r8 = 0x80000004 (address of keyboard data register)
LB r11, 0(r8) # reads the data from the keyboard, puts it in r11.

1
Computer Hardware Lab

This program will read a line of text from the keyboard and store it in RAM data memory

# int readWithoutEcho(char *p)


# This function reads characters from the keyboard, storing them into memory at address p,
# repeatedly until the user types a newline. The newline is discarded, but a nul (zero)
# terminator is put at the end of the string in memory. This function returns a count of
# how many characters were stored in memory, not counting the final zero.
# Registers modified:
# r2 - for the return value, n
# r4 - for parameter p (this is modified by the function)
# f8 - to hold special address 0x8000000, a
# r9 - status or character from keyboard, c
# r10 - temporary, to hold an ascii newline characters
readWithoutEcho:
addiu $2, $0, 0 # n = 0 // this is our counter
lui $8, 0x8000 # a = 0x80000000 // used for memory-mapped I/O
readKbdStatusLoop: # do {
lb $9, 0($8) # c = load from keyboard status register
beq $0, $9, readKbdStatusLoop # } while (kbd status c was zero)
lb $9, 4($8) # c = load from keyboard data register
addiu $10, $0, 0x0a # temp = ascii newline
beq $9, $10, stopReading # if (c == newline) go to stopReading
sb $9, 0($4) # *p = c // stores this character into memory
addiu $4, $4, 1 # p++ // advance pointer to next address
addiu $2, $2, 1 # n++
j readKbdStatusLoop # go back to nearly the top and do it all again
stopReading:
sb $0, 0($4) # *p = 0 // store a terminating zero into memory
jr $31 # return

Test with test program


main:
addiu $4, $0, 0x30 # argument is memory location 0x30
jal readWithoutEcho # jumps to your readWithoutEcho code
nop
sw $2, 0($0) # copy result to memory location 0
nop
addiu $4, $0, 0x90 # argument is memory location 0x90
jal readWithoutEcho # jumps to your readWithoutEcho code a second time
nop
sw $2, 4($0) # copy result to memory location 4
nop
# at this point, whatever two strings you entered at the keyboard should
# be in memory at locations 0x30 and 0x90, and memory locations 0 and 4
# should contain the lengths of the two strings.
infiniteloop:
j infiniteloop # deliberate infinite loop to stop the program

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.

- The characters entered by the user will be displayed on the console.

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

The function is used in this test program

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

printString("Please type a number: ")


readString(s) // where s is some other address of your choosing
n = atoi(s)
printString("Please type a secret sentence: ")
readWithoutEcho(p) // where p is some address of your choosing
for (each character c of string p) {
if (c >= ascii "a") and (c <= ascii "z") {
c = c + n
while (c > ascii "z") // if we went off the end of alphabet
c = c - 26 // subtract, repeatedly, to get back into alphabet
while (c < ascii "a") // if we went off the end of alphabet
c = c + 26 // add, repeatedly, to get back into alphabet
}
store c back into the string
}
printString("Encoded message is: ")
printString(p)

The expected result:

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!

You might also like