-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.asm
More file actions
150 lines (122 loc) · 4.6 KB
/
Copy pathmain.asm
File metadata and controls
150 lines (122 loc) · 4.6 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
.data #data declaration section
userInput: .space 9
errorMessage: .asciiz "Invalid hexadecimal number"
.text #assembly language instructions
main:
#gets input from the user
li $v0, 8 #system call for getting input from the user
la $a0, userInput #loads the address of userInput into $a0
li $a1, 9 #specifies the number of bytes to read
syscall
#reading character in a string
la $s1, userInput
li $t3, 0 #initializing sum to be 0
lb $t2, 0($s1) #loads the first character in userInput to $t2
beq $t2, 10, invalidMessage #if the first character is newLine, then go to invalidMessage
#checks for spaces at the front and end of string and removes it
li $t6, 0 #initializing $t7 to be 0
loopTempEnd:
add $t0, $s1, $t6
lb $t2, 0($t0) #loads the first character in userInput to $t2
beq $t2, $zero, continue
li $s2, 10
beq $t2, $s2, continue
addi $t6, $t6, 1
j loopTempEnd
#tempEnd is in $t6
continue:
li $s2, 0
loopStartIndex:
add $t0, $s1, $s2
lb $t2, 0($t0)
bne $t2, ' ', loopEndIndex
addi $s2, $s2, 1
j loopStartIndex
#startIndex is in $s2
loopEndIndex:
add $t0, $s1, $t6
lb $t2, 0($t0)
bne $t2, ' ', continue1
addi $t6, $t6, -1
j loopEndIndex
#endIndex is in $t6
continue1:
#addi $t8, $zero, 0 #initializing $t8 to be 0
loop:
add $t0, $s1, $s2
lb $t2, 0($t0) #loads the first character in userInput to $t2
#beq $t2, $zero, output #if current character in $t2 == $zero i.e the char code of endline
#beq $t2, 10, output #if $t2 == newline character then go to signedToUnsigned
jal checkChar #calls the checkChar function
beq $v1, $zero, invalidMessage #calls Exit if the char is invalid
jal conversion #calls conversion function to convert hex to dec
sll $t3, $t3, 4 #shifts $t3 left by 4 bits
add $t3, $t3, $t2 #adds $t5 to $t3 and stores the result in $t3
addi $s2, $s2, 1 #incrementing $t1 by 1
beq $s2, $t6, output
#add $t0, $t0, 1 #incremets the address to get next character #increment offset
j loop #loops again
Exit:
li $v0, 10
syscall #informs system to end program
#loop1:
#sll $t0, $t0, 1
#jr $ra
#prints an error message
invalidMessage:
li $v0, 4 #system call code for printing string
la $a0, errorMessage #loads address of errorMessage to $a0
syscall #syscall to print
j Exit #jumps to Exit
#outputs the unsigned or signed decimal value to the screen
output:
blt $t3, $zero, signedToUnsigned #branch to signedToUnsigned if $t3 < $zero
li $v0, 1 #syscall for printing integer
addi $a0, $t3, 0 #adds contents of $t3 and 0 and stores in $a0
syscall
j Exit #jumps to Exit
signedToUnsigned:
li $t1, 10 #initiates $t1 = 10
divu $t3, $t1 #divides $t4 by $t1
mflo $t2 #contents of $LO are moved to $t2
move $a0, $t2 #moves contents of $a0 to $t2
li $v0, 1 #system call code for printing integer
syscall
mfhi $t2 #contents of $HI are moved to $t2
move $a0, $t2 #moves contents of $t2 to $a0
li $v0, 1 #system call code for priting integer
syscall
j Exit
#checks validity of the characters
checkChar:
bgt $t2, 102, invalid #jumps to invalid if value at $t5 > 102
bgt $t2, 96, valid #jumps to valid if value at $t5 > 96
bgt $t2, 70, invalid #jumps to invalid if value at $t5 > 70
bgt $t2, 64, valid #jumps to valid if value at $t5 > 57
bgt $t2, 57, invalid #jumps to invalid if value at $t5 > 57
bgt $t2, 47, valid #jumps to valid if value at $t5 > 47
j invalid #jumps to invalid
invalid:
li $v1, 0 #initiates $v1 to be 0
jr $ra
valid:
li $v1, 1 #inititates $v1 to be 1
jr $ra
#converts hexadecimal number to its decimal value
conversion:
#addi $t6, $t6, 1
blt $t2, 58, conv1 #branches to conv1 if value at $t5 < 58
blt $t2, 71, conv2 #branches to conv2 if value at $t5 < 71
blt $t2, 103, conv3 #branches to conv3 if value at $t5 < 103
#converts hexadecimal values from 0-9 into decimal
conv1:
addi $t2, $t2, -48 #adds -48 to $t5 and stores it in $t5
jr $ra #jumps to statements whose address is $ra
#converts hexadecimal values from A-F into decimal values
conv2:
addi $t2, $t2, -55 #addis -55 to $t5 and stores it in $t5
jr $ra
#converts hexadecimal values from a-f into decimal values
conv3:
addi $t2, $t2, -87 #adds -87 to $t5 and stores it in $t5
jr $ra