Skip to content

Instantly share code, notes, and snippets.

@ejbs
Created September 12, 2016 19:59
Show Gist options
  • Save ejbs/7b535e14c595d50b39d9ee85990a6ddf to your computer and use it in GitHub Desktop.
Save ejbs/7b535e14c595d50b39d9ee85990a6ddf to your computer and use it in GitHub Desktop.
DTEK
# hexmain.asm
# Written 2015-09-04 by F Lundevall
# Copyright abandonded - this file is in the public domain.
# Q1: Print 0, since it cleans out the bits left of the fourth bit
# Q2: For numbers <9
.text
main:
li $a0,0x0F # change this to test different values
jal hexasc # call hexasc
nop # delay slot filler (just in case)
move $a0,$v0 # copy return value to argument register
li $v0,11 # syscall with v0 = 11 will print out
syscall # one byte from a0 to the Run I/O window
stop: j stop # stop after one run
nop # delay slot filler (just in case)
hexasc:
# Remove all bits but the 4 LSB saved to t0 from a0, ignore checking for negative number
sll $t0, $a0, 28
srl $t0, $t0, 28
li $t1, 0x09
ble $t0, $t1, number
letter:
# Remove so we get back to 0-9 range
addi $t0, $t0, -0x0A
# Add on 0x41 to start at 'A'
addi $t0, $t0, 0x41
j finish
number:
# Add on 0x30 to start at '0'
addi $t0, $t0, 0x30
j finish
finish:
li $v0, 0
add $v0, $0, $t0
jr $ra
# timetemplate.asm
# Written 2015 by F Lundevall
# Copyright abandonded - this file is in the public domain.
#ASS3 Q:
# 1. We save all args, ra, gp, sp and fp because we have to (ra, sp and fp are important to restore our place in the stack and where we return to)
# We save the t registers we use because we need them later on
# We save the s registers because we're forced to by convention
# 2. None.
# 3. 133,162-166
.macro PUSH (%reg)
addi $sp,$sp,-4
sw %reg,0($sp)
.end_macro
.macro POP (%reg)
lw %reg,0($sp)
addi $sp,$sp,4
.end_macro
.macro PUSHALL()
PUSH $a1
PUSH $ra
PUSH $t1
PUSH $t2
PUSH $t3
PUSH $t4
PUSH $a0
PUSH $gp
PUSH $sp
PUSH $fp
#$s0 - $s7
PUSH $s0
PUSH $s1
PUSH $s2
PUSH $s3
PUSH $s4
PUSH $s5
PUSH $s6
PUSH $s7
.end_macro
.macro POPALL()
POP $s7
POP $s6
POP $s5
POP $s4
POP $s3
POP $s2
POP $s1
POP $s0
POP $fp
POP $sp
POP $gp
POP $a0
POP $t4
POP $t3
POP $t2
POP $t1
POP $ra
POP $a1
.end_macro
.data
.align 2
mytime: .word 0x5957
colonASCII: .byte 0x3A
timstr: .ascii "text more text lots of text\0"
.text
main:
# print timstr
la $a0,timstr
li $v0,4
syscall
nop
# wait a little
li $a0,2
jal delay
nop
# call tick
la $a0,mytime
jal tick
nop
# call your function time2string
la $a0,timstr
la $t0,mytime
lw $a1,0($t0)
jal time2string
nop
# print a newline
li $a0,10
li $v0,11
syscall
nop
# go back and do it all again
j main
nop
# tick: update time pointed to by $a0
tick: lw $t0,0($a0) # get time
addiu $t0,$t0,1 # increase
andi $t1,$t0,0xf # check lowest digit
sltiu $t2,$t1,0xa # if digit < a, okay
bnez $t2,tiend
nop
addiu $t0,$t0,0x6 # adjust lowest digit
andi $t1,$t0,0xf0 # check next digit
sltiu $t2,$t1,0x60 # if digit < 6, okay
bnez $t2,tiend
nop
addiu $t0,$t0,0xa0 # adjust digit
andi $t1,$t0,0xf00 # check minute digit
sltiu $t2,$t1,0xa00 # if digit < a, okay
bnez $t2,tiend
nop
addiu $t0,$t0,0x600 # adjust digit
andi $t1,$t0,0xf000 # check last digit
sltiu $t2,$t1,0x6000 # if digit < 6, okay
bnez $t2,tiend
nop
addiu $t0,$t0,0xa000 # adjust last digit
tiend: sw $t0,0($a0) # save updated result
jr $ra # return
nop
# you can write your code for subroutine "hexasc" below this line
#
time2string:
add $t0, $a1, $0 #load timeStr into $t0
sll $t0, $t0, 16 #null left of 16bits
srl $t0, $t0, 16
# zero out everything except valued bits
andi $t1, $t0, 0x000F #sec1
andi $t2, $t0, 0x00F0 #sec2
andi $t3, $t0, 0x0F00 #min1
andi $t4, $t0, 0xF000 #min2
# Move them all down
srl $t1, $t1, 0
srl $t2, $t2, 4
srl $t3, $t3, 8
srl $t4, $t4, 12
# Let's send them to hexasc and fill in the memory!
# Oh right, it doesn't matter which way we do it, gotta fix the indices x)
PUSHALL
add $a0, $t4, $0
jal hexasc
POPALL
sb $v0, 0($a0)
PUSHALL
add $a0, $t3, $0
jal hexasc
POPALL
sb $v0, 1($a0)
#Add colon
la $t0, colonASCII
sb $t0 2($a0)
PUSHALL
add $a0, $t2, $0
jal hexasc
POPALL
sb $v0, 3($a0)
PUSHALL
add $a0, $t1,$0
jal hexasc
POPALL
sb $v0, 4($a0)
# NUL
addi $t0, $0, 0x00
sb $t0, 5($a0)
jr $ra
hexasc:
# Remove all bits but the 4 LSB saved to t0 from a0, ignore checking for negative number
sll $t0, $a0, 28
srl $t0, $t0, 28
li $t1, 0x09
ble $t0, $t1, number
letter:
# Remove so we get back to 0-9 range
addi $t0, $t0, -0x0A
# Add on 0x41 to start at 'A'
addi $t0, $t0, 0x41
j finish
number:
# Add on 0x30 to start at '0'
addi $t0, $t0, 0x30
j finish
finish:
li $v0, 0
add $v0, $0, $t0
jr $ra
delay:
jr $ra
nop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment