Professional Documents
Culture Documents
MehmetEmre Kantaş CS224-001 PRELIM Lab2
MehmetEmre Kantaş CS224-001 PRELIM Lab2
MehmetEmre Kantaş CS224-001 PRELIM Lab2
#convertHextoDec.asm
.text
main:
#printing welcome message
la $a0, welcome
li $v0, 4
syscall
#loading register a0 with the address of the first element of the char array
la $a0, hexStringAddress
jal convertHexToDecimal
convertHexToDecimal:
move $s0, $a0 #loading register s0 with the beginning address of the
string
nextChar:
endOfTheString:
calculateDecimalValue:
asciiToDecimalValue:
decrement_abcdef:
continue1:
decrementABCDEF:
continue2:
adding:
notValidHexadecimal:
finish:
la $a0, newLine
li $v0, 4
syscall
j loop
quit:
la $a0, goodbye
li $v0, 4
syscall
li $v0, 10
syscall
.data
hexStringAddress:
.space 8
welcome:
.asciiz "Welcome to the hexadecimal to decimal converter!\n"
enter:
.asciiz "\nEnter the hexadecimal number(max 8 digits)(0 to quit): "
decimal:
.asciiz "Decimal: "
goodbye:
.asciiz "Goodbye!"
invalid:
.asciiz "Invalid input!\n"
newLine:
.asciiz "\n"
##############################END OF PRELIMINARY
PART1##############################
#################################PRELIMINARY
PART2##################################
#invertByte.asm
.text
main:
loop:
#prompting the user
la $a0, enter
li $v0, 4
syscall
#taking input
la $a0, input
li $v0, 5
syscall
la $s3, hexaArray
convertToHex:
blt $s1, 0, done #to take power and control the loop
move $a0, $s1 #loading the argument (power will return
16^$a0 in register $v0)
jal power
li $t0, 0 #t0 will be the num on index s1 + 1
howMany:
found:
bgt $t0, 9, addHexa #if ABCDEF (10, 11, 12, 13, 14,
15), branch to addHexa
ble $t0, 9, addNum #if 0123456789, branch to addNum
addHexa:
addA:
sb $t0, 0($s3)
j added
addB:
sb $t0, 0($s3)
j added
addC:
sb $t0, 0($s3)
j added
addD:
sb $t0, 0($s3)
j added
addE:
sb $t0, 0($s3)
j added
addF:
sb $t0, 0($s3)
j added
addNum:
added:
done:
la $a0, hexaArray
li $v0, 4
syscall
la $a0, InvHexaForm
li $v0, 4
syscall
la $a0, 0($s3)
la $a1, 6($s3)
jal swap
la $a0, 1($s3)
la $a1, 7($s3)
jal swap
la $a0, 2($s3)
la $a1, 4($s3)
jal swap
la $a0, 3($s3)
la $a1, 5($s3)
jal swap
quit:
swap:
jr $ra
power:
move $t0, $a0 #register t0 holds the power (it is also a loop
control)
li $v0, 1 #register v0 will be the return value
loopPower:
.data
input:
.word 0
welcome:
.asciiz "Welcome to byte inverter!"
enter:
.asciiz "\n\nEnter a number in decimal(max 2,147,483,647 = (2^31) -
1)(0 to quit): "
hexaForm:
.asciiz "Hexadecimal form: "
InvHexaForm:
.asciiz "\nInverted hexadecimal form: "
goodbye:
.asciiz "Goodbye!"
hexaArray:
.space 8