summaryrefslogtreecommitdiff
path: root/PGU/CHAP10/int2str.s
blob: 6ea5178179d9378f5754f96344cf79c544b8abfc (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# integer2string
#
# Convert an int number to a decima string for display
#
# Receives a buffer large enough to hold the largest possible numberr
# and an integer to convert
#
# %rcx hold the count of characters processed
# %rax holds the current value
# %rdi hold the base (10)

.equ ST_VALUE, 16
.equ ST_BUFFER, 24

.globl integer2string
.type integer2string, @function

integer2string:

	# Default function setup
	pushq %rbp
	movq %rsp, %rbp

	movq $0, %rcx	# Init char count
	movq ST_VALUE(%rbp), %rax	# Retrieve value from stack

	# The divisor must be in a register or memory location
	movq $10, %rdi

	conversion_loop:
		# Division is actually performed on the combined
		# %rdx:%rax register, so first, clear out %rdx
		movq $0, %rdx

		# Divide %rdx:%rax (which are implied) by 10
		# Store the quotient in %rax and the remainder in
		# %rdx (both which are implied)
		divq %rdi

		# Quotient is in the right place. %rdx has the remainder
		# which now needs to be converted into a number. So, %rdx has a
		# number that is 0 through 9. We'll use it as an 'index' on the
		# ASCII table starting from char '0'.
		addq $'0', %rdx	# Get our number's index from ASCII
					# table, starting from '0'.

		# Push this value into the stack. When we are done, we can just
		# pop them off one-by-one, and they will be in the right order.
		# NOTE: We are pushing the whole register, but we only need the
		# byte in %dl (The last byte of the %rdx register) for the
		# character.
		pushq %rdx

		incq %rcx	# Increment digit count

		cmpq $0, %rax	# Have we reached the end of the calculation?
		je end_conversion_loop

		# %rax has its new value
		jmp conversion_loop

	end_conversion_loop:
		# The string is now on the stack, if we pop it off a char at a
		# time, we can copy it into the buffer and we are done.

		movq ST_BUFFER(%rbp), %rdx	# Move buffer address to %rdx

	copy_reversing_loop:
		popq %rax
		movb %al, (%rdx)
		decq %rcx	# Once we reach 0 we are finished
		incq %rdx	# Point to the next byte

		cmpq $0, %rcx	# Check if we are finished
		je end_copy_reversing_loop

		jmp copy_reversing_loop

	end_copy_reversing_loop:
		movb $0, (%rdx)

	movq %rbp, %rsp
	popq %rbp
	ret

# end of integer2string