about summary refs log tree commit diff
path: root/lang/mips/chapter-3/exercise-6.s
blob: 8c1b6fd83ffac5228da150ff3cf6c290097d8dd4 (plain) (blame)
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
# print(hex(0b10101))
	.text
main:
	li	$t0,	21		# t0 = 0b10101
	li	$t1,	0		# t1 = 0
	li	$t9,	9		# t9 = 9

reverse:
	beqz	$t0,	done		# if (!t0) goto done
	andi	$t2,	$t0,	0xf	# t2 = t0 & 0xf
	sra	$t0,	$t0,	4	# t0 >>= 4
	sll	$t1,	$t1,	4	# t1 <<= 4
	add	$t1,	$t1,	$t2	# t1 += t2
	j	reverse			# goto reverse
done:

	li	$v0,	11		# print character
	li	$a0,	48		# 0
	syscall
	li	$v0,	11		# print character
	li	$a0,	120		# x
	syscall
	bnez	$t1,	print		# if (!t1) goto print
	li	$v0,	11		# print character
	li	$a0,	48		# 0
	syscall
	j	end			# goto end

print:
	beqz	$t1,	end		# if (!t1) goto end
	andi	$t2,	$t1,	0xf	# t2 = t1 & 0xf
	sra	$t1,	$t1,	4	# t1 >>= 4
	addi	$a0,	$t2,	48	# a0 = chr(t2), sort of
	ble	$t2,	$t9,	put	# if (t2 <= 9) goto put
	addi	$a0,	$a0,	7	# a0 += 7
put:
	li	$v0,	11		# print character at a0
	syscall
	j	print			# goto print
end:

	li	$v0,	11		# print character
	li	$a0,	10		# newline
	syscall

	li	$v0,	10		# terminate
	syscall