0

I need help converting numbers to ASCII. This is my code so far:

.global itoascii

itoascii:

    /* Initialize variables */
    mov x1, #10               /* Divisor */
    mov x2, buffer            /* Buffer address */

convert_loop:
    udiv x3, x0, x1           /* Divide the number by 10, quotient in x3, remainder in x0 */
    add x4, x0, #'0'          /* Convert remainder to ASCII */
    strb w4, [x2], #1         /* Store ASCII character in buffer and increment buffer pointer */

    cmp x3, #0                /* Check if quotient is zero */
    beq end_conversion        /* If quotient is zero, exit the loop */

    mov x0, x3                /* Update the number with the quotient */
    b convert_loop            /* Repeat the process */

end_conversion:
    mov x0, x2                /* Return the address of the buffer */
    ret

.data
    /* Put the converted string into buffer,
       and return the address of buffer */
    buffer: .fill 128, 1, 0
3
  • Welcome to Stack Overflow! Some general tips: (1) Format code blocks using ``` on its own line at the beginning and end. See stackoverflow.com/help/formatting for more info on formatting. (2) Use the assembly tag for assembly language questions, as well as a tag for the specific architecture you are programming for; armv8 is okay but arm64 gets more attention. Commented Nov 18, 2023 at 16:51
  • 1
    That would store digits in reverse order. Start from the end of the buffer and loop backwards. Also, you need the remainder as the digit (with msub using the quotient, divisor, and original dividend.) See How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string) for the algorithm in C which you could compile for AArch64. Also, mov doesn't work to put a label address into a register in AArch64. Commented Nov 18, 2023 at 16:51
  • 1
    (3) In general, you should ask specific questions, rather than "I need to write this, please help." Explain what you have tested, what went wrong (exact output and/or error messages), what you tried to fix it, etc. People here will help you figure out your problem, but they won't just do it for you. Commented Nov 18, 2023 at 16:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.