0% found this document useful (0 votes)
37 views7 pages

MehmetEmre Kantaş CS224-001 PRELIM Lab2

This document contains assembly code that performs two tasks: 1) It converts a hexadecimal string to its decimal equivalent by iterating through each character, determining its numeric value, and calculating its place value in the decimal result. 2) It takes a decimal number as input, converts it to hexadecimal, inverts the nibbles to swap each pair of characters, and outputs the inverted hexadecimal string. It uses loops, conditionals, and math operations like multiplication and subtraction to perform the conversions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views7 pages

MehmetEmre Kantaş CS224-001 PRELIM Lab2

This document contains assembly code that performs two tasks: 1) It converts a hexadecimal string to its decimal equivalent by iterating through each character, determining its numeric value, and calculating its place value in the decimal result. 2) It takes a decimal number as input, converts it to hexadecimal, inverts the nibbles to swap each pair of characters, and outputs the inverted hexadecimal string. It uses loops, conditionals, and math operations like multiplication and subtraction to perform the conversions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

##############################PRELIMINARY PART1##############################

#convertHextoDec.asm
.text
main:
#printing welcome message
la $a0, welcome
li $v0, 4
syscall

#prompting the user to enter a hexadecimal number and enter 0 to terminate


the program
loop:
la $a0, enter
li $v0, 4
syscall

#taking hexadecimal input as a char array (string)


la $a0, hexStringAddress
li $a1, 10
li $v0, 8
syscall

#loading register a0 with the address of the first element of the char array
la $a0, hexStringAddress

#checking if the user entered 0


lbu $t0, 0($a0)
subi $t0, $t0, 48
beq $t0, 0, quit

jal convertHexToDecimal

convertHexToDecimal:

move $s0, $a0 #loading register s0 with the beginning address of the
string

nextChar:

lbu $s1, 0($s0) #getting the data in the address in


register s0 and storing it into register s1
blt $s1, 10, endOfTheString #if the value in s1 is less then 10,
continue (ascii of new line is 10)
addi $s0, $s0, 1 #to obtain the next char in the string
j nextChar

endOfTheString:

subi $s0, $s0, 2 #setting the address in register s0 to the


least significant bit of the string

li $s2, 1 #to multiply with the current digit to find the


decimal value of the digit
li $s3, 16 #to multiply with s2
li $s4, 0

calculateDecimalValue:

blt $s0, $a0, finish #stop evaluating characters if the


address in s0 is less than the address in a0
lbu $s1, 0($s0) #getting the value in the address in the
register s0
blt $s1, 48, notValidHexadecimal #
blt $s1, 58, asciiToDecimalValue #
blt $s1, 65, notValidHexadecimal #
blt $s1, 71, asciiToDecimalValue # checking if the char is a
valid hexadecimal number
blt $s1, 97, notValidHexadecimal #
blt $s1, 103, asciiToDecimalValue #
bge $s1, 103, notValidHexadecimal #

asciiToDecimalValue:

bgt $s1, 70, decrement_abcdef #for abcdef


j continue1

decrement_abcdef:

subi $s1, $s1, 32 #converting abcdef to ABCDEF

continue1:

bgt $s1, 57, decrementABCDEF #keep processing ABCDEF


j continue2

decrementABCDEF:

subi $s1, $s1, 55 #converting ABCDEF to 10, 11, 12,


13, 14 and 15, respectively
j adding

continue2:

subi $s1, $s1, 48 #finding decimal value of 0123456789


(before this point, they were ascii values)

adding:

mul $s1, $s2, $s1 #multiplying the decimal value with


2^(digit number - 1) to find its contribution to sum
add $s4, $s4, $s1 #calculating the sum
subi $s0, $s0, 1 #to obtain the next char
mul $s2, $s2, $s3 #digit coefficient for the next char
j calculateDecimalValue

notValidHexadecimal:

#informing user that they input an incalid value


la $a0, invalid
li $v0, 4
syscall
j loop

finish:

#printing the result


la $a0, decimal
li $v0, 4
syscall

move $a0, $s4


li $v0, 1
syscall

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:

#printing welcome message


la $a0, welcome
li $v0, 4
syscall

loop:
#prompting the user
la $a0, enter
li $v0, 4
syscall

#taking input
la $a0, input
li $v0, 5
syscall

#quit if user input is 0


beq $v0, 0, quit

#storing the input in a variable and taking a copy to register s0


sw $v0, input
lw $s0, input

li $s1, 7 #s1 is the max power


li $s2, 0 #s2 will be the hexadecimal sum

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:

blt $s0, $v0, found #taking the number values of


each digit
sub $s0, $s0, $v0 #by subtracting 16^(digitNo - 1)
addi $t0, $t0, 1 #and saving how many times did we
do the subtraction
j howMany #we save the number value of the
digit in register t0

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:

addi $t0, $t0, 55 #getting decimal values of the


hexadecimals

beq $t0, 65, addA #


beq $t0, 66, addB #
beq $t0, 67, addC #Deciding what to add on string
beq $t0, 68, addD #
beq $t0, 69, addE #
beq $t0, 70, addF #

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:

addi $t0, $t0, 48 #if $t0 is 0123456789, find its decimal


value
sb $t0, 0($s3) #adding it to the string

added:

addi $s1, $s1, -1 #changing power and control temporary


addi $s3, $s3, 1 #next char of the string
blt $s1, 0, done #if at the end of the input, branch to
done

j convertToHex #if not, loop

done:

#printing the hexadecimal form of the decimal input


la $a0, hexaForm
li $v0, 4
syscall

la $a0, hexaArray
li $v0, 4
syscall

#swapping nibbles to invert bytes


la $s3, hexaArray

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

#printing inverted version


la $a0, hexaArray
li $v0, 4
syscall
j loop

quit:

#printing goodbye message


la $a0, goodbye
li $v0, 4
syscall

#terminating the flow


li $v0, 10
syscall

swap:

lbu $t0, 0($a0) #temp1 = arr[1]


lbu $t1, 0($a1) #temp2 = arr[2]

sb $t0, 0($a1) #arr[1] = temp2


sb $t1, 0($a0) #arr[2] = temp1

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:

beq $t0, 0, donePower #when power = 0, done


mul $v0, $v0, 16 #taking 16^power
addi $t0, $t0, -1 #loop control
j loopPower
donePower: jr $ra

.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

You might also like