[Assembly] Binary to decimal conversion for 64bit> numbers
Say you have a value (%rsp):8(%rsp) how would you go about printing this number to stdout in decimal form?
One method would be dividing it by 10 of course recursively but I cant figure out how to apply a divide operation across a 128bit number (using 64bit registers). To test this I have a little program that's doubling the value 2^64 - 2 (18446744073709551614) hens the value when doubled is 2 ^ 65 - 4 (36893488147419103228) and would be stored
Converting from decimal to ascii is no problem, and in fact I think the best way to do this is to use printf. Essentially the number has to be converted so 8(%rsp) represents the first 64 decimal bits of the number and (%rsp) would have the binary representation for the most significant decimal part. Eg the number is 36893488147419103228 so after the conversion 8(%rsp) would contain 6893488147419103228 (in binary) and (%rsp) would contain 3.
.type main, @function
movq %rsp, %rbp
subq $8, %rsp
movq $18446744073709551614, %r13 #our number
# movq %r13, (%rsp)
addq %r13, %r13
movq $1, %rbx
movq %rbx, (%rsp)
addq $8, %rsp
movq %r13, (%rsp)
###Wanting to print number in decimal, I can output the binary by a recursive div2 operation. Here the value is at -8(%rsp):(%rsp).
movl $0, %eax
Gets converted to this.... (%rsp doesn't necessarily have to refer to the same location)
This would allow two printf statements to be executed. There are alternative methods, of course.
edit -- Btw this program is linked with g++ so `as ./file.s -o file.o && g++ file.o -o file` is how you assembly / link it.