Created
November 8, 2022 04:36
-
-
Save mgaudet/777aaea3836bed27f68908f0d4fd1f1e to your computer and use it in GitHub Desktop.
displaydemo.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------- | |
# MultiTimer Common File | |
# | |
# Author: Zachary Selk | |
# Date: Jul 18, 2019 | |
#------------------------------------------------------------------------------- | |
.data | |
.align 2 | |
DISPLAY_CONTROL: .word 0xFFFF0008 | |
DISPLAY_DATA: .word 0xFFFF000C | |
String: .asciz "Hello World!" | |
INTERRUPT_ERROR: .asciz "Error: Unhandled interrupt with exception code: " | |
INSTRUCTION_ERROR: .asciz "\n Originating from the instruction at address: " | |
.text | |
main: | |
# Print String | |
la t0, String | |
mv a0, t0 # a0 <- strAddr | |
li a1, 0 # a1 <- row = i | |
li a2, 0 # a2 <- col = 0 | |
jal printStr | |
# Terminate | |
li a7, 10 | |
ecall | |
#------------------------------------------------------------------------------ | |
# printStr | |
# Args: | |
# a0: strAddr - The address of the null-terminated string to be printed. | |
# a1: row - The row to print on. | |
# a2: col - The column to start printing on. | |
# | |
# Prints a string in the Keyboard and Display MMIO Simulator terminal at the | |
# given row and column. | |
#------------------------------------------------------------------------------ | |
printStr: | |
# Stack | |
addi sp, sp, -16 | |
sw ra, 0(sp) | |
sw s0, 4(sp) | |
sw s1, 8(sp) | |
sw s2, 12(sp) | |
mv s0, a0 | |
mv s1, a1 | |
mv s2, a2 | |
printStrLoop: | |
# Check for null-character | |
lb t0, 0(s0) # t0 <- char = str[i] | |
# Loop while(str[i] != '\0') | |
beq t0, zero, printStrLoopEnd | |
# Print character | |
mv a0, t0 # a0 <- char | |
mv a1, s1 # a1 <- row | |
mv a2, s2 # a2 <- col | |
jal printChar | |
addi s0, s0, 1 # i++ | |
addi s2, s2, 1 # col++ | |
j printStrLoop | |
printStrLoopEnd: | |
# Unstack | |
lw ra, 0(sp) | |
lw s0, 4(sp) | |
lw s1, 8(sp) | |
lw s2, 12(sp) | |
addi sp, sp, 16 | |
jalr zero, ra, 0 | |
#------------------------------------------------------------------------------ | |
# printChar | |
# Args: | |
# a0: char - The character to print | |
# a1: row - The row to print the given character | |
# a2: col - The column to print the given character | |
# | |
# Prints a single character to the Keyboard and Display MMIO Simulator terminal | |
# at the given row and column. | |
#------------------------------------------------------------------------------ | |
printChar: | |
# Stack | |
addi sp, sp, -16 | |
sw ra, 0(sp) | |
sw s0, 4(sp) | |
sw s1, 8(sp) | |
sw s2, 12(sp) | |
# Save parameters | |
add s0, a0, zero | |
add s1, a1, zero | |
add s2, a2, zero | |
jal waitForDisplayReady # Wait for display before printing | |
# Load bell and position into a register | |
addi t0, zero, 7 # Bell ascii | |
slli s1, s1, 8 # Shift row into position | |
slli s2, s2, 20 # Shift col into position | |
or t0, t0, s1 | |
or t0, t0, s2 # Combine ascii, row, & col | |
# Move cursor | |
lw t1, DISPLAY_DATA | |
sw t0, 0(t1) | |
jal waitForDisplayReady # Wait for display before printing | |
# Print char | |
lw t0, DISPLAY_DATA | |
sw s0, 0(t0) | |
# Unstack | |
lw ra, 0(sp) | |
lw s0, 4(sp) | |
lw s1, 8(sp) | |
lw s2, 12(sp) | |
addi sp, sp, 16 | |
jalr zero, ra, 0 | |
#------------------------------------------------------------------------------ | |
# waitForDisplayReady | |
# | |
# A method that will check if the Keyboard and Display MMIO Simulator terminal | |
# can be writen to, busy-waiting until it can. | |
#------------------------------------------------------------------------------ | |
waitForDisplayReady: | |
# Loop while display ready bit is zero | |
lw t0, DISPLAY_CONTROL | |
lw t0, 0(t0) | |
andi t0, t0, 1 | |
beq t0, zero, waitForDisplayReady | |
jalr zero, ra, 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment