print int as ascii in assembly 64 bit at&t float n

2019-03-04 03:19发布

问题:

I've written a solo program just to figure out how to do this, earlier I added 48 to the number to print but now when I get numbers larger than 9 it's not really sufficient any more.

So the following program should put 23 into the my temp, compares 23 to 10 if it's smaller than 10 I can print otherwise put the temp away in rax, put 10 in r11 and divide 23 by 10. Put the result from rax in temp and check again. I still need to add a way to retrieve the earlier numbers but first thing first. Have I missunderstood what happens, the result I get is float number error

#include <stdio.h>          #Inkludera standard I/O

.data                   #Specifiera data
temp:           .quad   0
temp2:          .quad   0

.text   

  .global main
main:
movq    $23, temp
jmp check

check:
cmpq    $10, $temp
jl  under10
movq    $temp, %rax
movq    $10, %r11
divq    %r11
movq    %rax,temp
jmp check

under10:
addq    $48, temp
movq    $temp,%rdi
call    puts

回答1:

When you do divq %r11 you're actually dividing the 128-bit register pair rdx:rax by r11. So to get a quotient that will fit in rax you'll typically want to clear rdx before the division, i.e. before the divq, add an:

xorq %rdx, %rdx   # rdx = 0