//----------------------------------------------------------------- // rax2uint.s // // Here we present our assembly language code for generating // the string of digit-characters which represents the value // found in register RAX as unsigned integer in the base ten // number system. We assume that the address for the string // of characters is passed to our procedure in register RDI. // // to assemble: $ as rax2uint.s -o rax2uint.o // // NOTE: We declare our label for this routine's entry-point // to be a 'global' symbol so that we can link our code with // object-files for programs that wish to call this routine. // // programmer: ALLAN CRUSE // written on: 04 FEB 2009 //----------------------------------------------------------------- .section .text rax2uint: # # Convert the RAX value to a string of decimal numerals at DS:RDI. # # EXPECTS: RAX = the value to be converted # RDI = an address for the string # push %rax # save the caller's registers push %rbx push %rcx push %rdx push %rdi mov $10, %rbx # base of the decimal system mov $0, %rcx # number of digits generated nxdiv: mov $0, %rdx # RAX extended to (RDX,RAX) div %rbx # divide by the number-base push %rdx # save remainder on the stack inc %rcx # and count this remainder cmp $0, %rax # was the quotient zero? jne nxdiv # no, do another division nxdgt: pop %rdx # else pop recent remainder add $'0', %dl # and convert to a numeral mov %dl, (%rdi) # store to memory-buffer inc %rdi # and advance buffer-pointer loop nxdgt # again for other remainders pop %rdi # recover saved registers pop %rdx pop %rcx pop %rbx pop %rax ret # and return to the caller .global rax2uint # make entry-point visible .end # nothing else to assemble