-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec_to_hex.asm
62 lines (47 loc) · 1.77 KB
/
dec_to_hex.asm
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
# Prints a number in hexadecimal, digit by digit.
#
# Written by Aris Efthymiou, 16/08/2005
# Based on hex.s program from U. of Manchester for the ARM ISA
.data
prompt1: .asciiz "Enter decimal number.\n"
outmsg: .asciiz "The number in hex representation is:\n"
.globl main
.text
main:
# prompt for input
li $v0, 4 #syscall code for print_string in the MIPS system.
la $a0, prompt1
syscall
# Get number from user
li $v0, 5 #syscall code for reading an integer from the user.
syscall
add $s0, $zero, $v0 # Keep the number in $s0,
# Output message
li $v0, 4
la $a0, outmsg
syscall
# set up the loop counter variable
li $t0, 8 # 8 hex digits in a 32-bit number
# Main loop
loop: srl $t1, $s0, 28 # get leftmost digit by shifting it
# to the 4 least significant bits of $t1
# The following instructions convert the number to a char
slti $t2, $t1, 10 # t2 is set to 1 if $t1 < 10
beq $t2, $0, over10
addi $t1, $t1, 48 # ASCII for '0' is 48
beq $t1, 48, iterate
j print
over10: addi $t1, $t1, 55 # convert to ASCII for A-F
# ASCII code for 'A' is 65
# Use 55 because $t1 is over 10
# Print one digit
print: li $v0, 11
add $a0, $zero, $t1
syscall # Print ASCII char in $a0
iterate: # Prepare for next iteration
sll $s0, $s0, 4 # Drop current leftmost digit
addi $t0, $t0, -1 # Update loop counter
bne $t0, $0, loop # Still not 0?, go to loop
# end the program
li $v0, 10
syscall