-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec_to_binary_final.asm
46 lines (34 loc) · 1007 Bytes
/
dec_to_binary_final.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
.data
binary: .space 32 # 32 bytes to store binary values
.text
main:
li $s0, 167 # Load immediate value 12 into $s0 (numerator)
li $s2, 2 # Load immediate value 2 into $s2 (divisor)
li $t0, -1 # Initialize $t0 as the array index
while:
beq $s0, $zero, print # Exit loop if $s0 (dividend) becomes zero
# Divide $s0 by $s2 and store the remainder in $s3
div $s0, $s2
mfhi $s3 # Remainder is stored in $s3
addi $t0, $t0, 1 # Increment the array index
# Store the remainder in the binary array
sb $s3, binary($t0) # Store in the array at the current index
# Print the remainder (optional)
#li $v0, 1
#move $a0, $s3
#syscall
# Update $s0 with the quotient for the next iteration
mflo $s0
j while
print:
blt $t0, 0, end
lb $s3, binary($t0)
subi $t0, $t0, 1
li $v0, 1
move $a0, $s3
syscall
j print
end:
# Exit the program
li $v0, 10
syscall