Skip to content

Instantly share code, notes, and snippets.

@lkebin
Last active August 16, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkebin/66565f98fdcaa6ba031447d568cd2d32 to your computer and use it in GitHub Desktop.
Save lkebin/66565f98fdcaa6ba031447d568cd2d32 to your computer and use it in GitHub Desktop.
Computer Organization
.data
uStr: .asciiz
"Alpha","Bravo","China","Delta","Echo","Foxtrot","Golf","Hotel",
"India","Juliet","Kilo","Lima","Mary","November","Oscar","Paper","Quebec",
"Research","Sierra","Tango","Uniform","Victor","Whisky","X-ray","Yankee","Zulu"
lStr: .asciiz
"alpha","bravo","china","delta","echo","foxtrot","golf","hotel","india",
"juliet","kilo","lima","mary","november","oscar","paper","quebec","research",
"sierra","tango","uniform","victor","whisky","x-ray","yankee","zulu"
cOffset: .word
0,6,12,18,24,29,37,42,48,54,61,66,71,76,85,91,97,104,113,120,126,134,141,148,154,161
numStr: .asciiz
"Zero","First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eighth","Ninth"
nOffset: .word
0,5,11,18,24,31,37,43,51,58
exitMsg: .asciiz "Bye bye!"
.text
.globl main
main:
li $v0, 12
syscall
# if the input charactor is '?' (ascii 63), then jump to exit section
beq $v0, '?', exit
# if the input charactor less than '0' (ascii 48), it is symble
blt $v0, '0', isSymble
# if the input charactor less than ':' (ascii 58), it is number, because number charactor ascii range is 48 - 57
blt $v0, ':', isNumber
# if the input charactor less than 'A' (ascii 65), it is symble, because ascii (58 - 64) contains charactor :,;,<,=,>,?,@
blt $v0, 'A', isSymble
# if the input charactor less than '[' (ascii 91), it is upper charactor, because upper charactor ascii range is 65 - 90
blt $v0, '[', isUpper
# if the input charactor less than 'a' (ascii 97), it is symble, because ascii (91 - 96) contains charactor [,\,],^,_,`
blt $v0, 'a', isSymble
# if the input charactor less than '{' (ascii 123), it is lower charactor, because lower charactor ascii range is 97 - 122
blt $v0, '{', isLower
j isSymble
isUpper:
# Get the number offset
sub $t1, $v0, 'A'
# Do number << 2
sll $t1, $t1, 2
la $s0, cOffset
add $s0, $s0, $t1
lw $s1, ($s0)
# Get the number string according to the number offset value
la $a0, uStr
add $a0, $a0, $s1
j printStr
isLower:
# Get the number offset
sub $t1, $v0, 'a'
sll $t1, $t1, 2
la $s0, cOffset
add $s0, $s0, $t1
lw $s1, ($s0)
# Get the number string according to the number offset value
la $a0, lStr
add $a0, $a0, $s1
j printStr
isNumber:
# Get the number offset
sub $t1, $v0, '0'
sll $t1, $t1, 2
la $s0, nOffset
add $s0, $s0, $t1
lw $s1, ($s0)
# Get the number string according to the number offset value
la $a0, numStr
add $a0, $a0, $s1
j printStr
printStr:
li $v0, 4
syscall
li $a0, '\n'
li $v0, 11
syscall
j main
isSymble:
# load '*' to $a0 register
li $a0, '*'
li $v0, 11
syscall
li $a0, '\n'
li $v0, 11
syscall
j main
exit:
li $a0, '\n'
li $v0, 11
syscall
# print string
la $a0, exitMsg
li $v0, 4
syscall
.data
buffer: .space 1024
successMsg: .asciiz "Success! Location:"
failMsg: .asciiz "Fail!"
exitMsg: .asciiz "Bye bye!"
.text
main:
# Read search source string
la $a0, buffer,
li $a1, 1024
li $v0, 8
syscall
move $s0, $a0
# Initial loop number
add $a3, $zero, $zero
add $t0, $zero, $zero
needleChar:
# Read search needle charactor
li $v0, 12
syscall
beq $v0, '?', exit
move $a2, $v0
search:
# If current iterate number equal to the max length, then jump to searchFail section
beq $t0, $a1, searchFail
la $a0, buffer
add $t2, $a0, $t0
lb $s1, ($t2)
beq $s1, $a2, searchSuccess
# Add 1 on the iterate number
add $t0, $t0, 1
j search
searchSuccess:
la $a0, successMsg
li $v0, 4
syscall
# Set the index value start from 1 rather than 0
add $a0, $t0, 1
li $v0, 1
syscall
add $a0, $zero, '\n'
li $v0, 11
syscall
j needleChar
searchFail:
la $a0, failMsg
li $v0, 4
syscall
add $a0, $zero, '\n'
li $v0, 11
syscall
# Reset loop number
add $a3, $zero, $zero
add $t0, $zero, $zero
j needleChar
exit:
li $a0, '\n'
li $v0, 11
syscall
# print string
la $a0, exitMsg
li $v0, 4
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment