Question: Q2: .data messageOne: .asciiz Enter binary number : messageSum: .asciiz Number in decimal is : empty: .space 16 .text .globl main main:

 Q2: .data messageOne: .asciiz "Enter binary number : " messageSum: .asciiz

Q2:

.data messageOne: .asciiz "Enter binary number : " messageSum: .asciiz " Number in decimal is : " empty: .space 16 .text .globl main

main:

getNum: li $v0,4 # Print string system call la $a0,messageOne #"Please insert value (A > 0) : " syscall

la $a0, empty li $a1, 16 # load 16 as max length to read into $a1 li $v0,8 # 8 is string system call syscall

li $t4, 0 # sum = 0

startConvert: la $t1, empty li $t9, 16 # counter = 16

firstByte: lb $a0, ($t1) # load 1st byte blt $a0, 48, printSum addi $t1, $t1, 1 # increment offset subi $a0, $a0, 48 # subtract 48 to convert to int value subi $t9, $t9, 1 # decrement counter beq $a0, 0, isZero beq $a0, 1, isOne j convert

isZero: j firstByte

isOne: li $t8, 1 sllv $t5, $t8, $t9 # shift left and store in $t5 add $t4, $t4, $t5 # add sum to previous sum

j firstByte

convert:

printSum: srlv $t4, $t4, $t9

la $a0, messageSum li $v0, 4 syscall

move $a0, $t4 #load li $v0, 1 # print syscall

exit: li $v0, 10 syscall

Q3. Modify your code from Q2 so as to display the hexadecimal representation of the binary number instead of the decimal value. Assume the user enters a valid binary number. (6 marks) Hints You need to print a single character at the end: either '0' to '9, or'A' to F' To convert from decimal to character representing a hex digit, add either 0x30 if the decimal value is from 0 to 9 or 0x37 if the decimal value is from 10 to 15. 0111 program is finished running (dropped off bottom)- 1110 program is finished running (dropped off bottom)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!