forked from thuongtruong1009/computer-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4_1_3.s
67 lines (58 loc) · 958 Bytes
/
lab4_1_3.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#Code by https://github.com/thuongtruong1009
.data
prompt: .asciiz "Input interger: "
output: .asciiz "Hexadecimal: "
error: .asciiz "Input must be in range (0-15)\n"
annotation: .asciiz "0x"
.text
.globl main
main:
la $a0, prompt
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
la $a0, output
li $v0, 4
syscall
move $a3, $t0
jal printHex32
printHex32If:
blt $t1, 8, printHex32Loop
li $v0, 10
syscall
printHex32:
la $a0, annotation
li $v0, 4
syscall
li $t1, 0
printHex32Loop:
rol $a3, $a3, 4
andi $t2, $a3, 0xF
move $a1, $t2
jal printHex
addi $t1, $t1, 1
j printHex32If
printHex:
bgt $a1, 15, printError
blt $a1, 0, printError
blt $a1, 10, printNum
j printChar
jr $ra
printNum:
move $a0, $a1
li $v0, 1
syscall
jr $ra
printChar:
addi $a1, $a1, 55
move $a0, $a1
li $v0, 11
syscall
jr $ra
printError:
la $a0, error
li $v0, 4
syscall
jr $ra