0

I want to iterate over the characters of a string and convert every single character to its respective ASCII value. So far I have created this program that takes input from the user and then stores it in the buffer. I have also created a loop that reads the character from the buffer (esi) and stores it within the al register. After supposedly converting the character in al to ASCII, it increments esi to go to the next character and runs the loop_start routine again.

section .data
    
    prompt db "Enter your message: "
    prompt_len equ $-prompt
    newline db 10, 0

section .bss

    buffer resb 255

section .text
_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, prompt_len
    int 0x80

    mov eax, 3
    mov ebx, 2
    mov ecx, buffer
    mov edx, 255
    int 0x80
    
    mov esi, buffer

loop_start:
    mov al, byte [esi]
    cmp al, 0
    je done

    cmp esi, buffer
    je print_char


print_char:

    ; do something  

    inc esi
    jmp loop_start

done:
    mov eax, 1
    mov ebx, 0
    int 0x80

    ret

How do I make it so that I am able to print out the ASCII value of the character as the loop iterates over it. Thank you in advance!

1nc0gn170
  • 48
  • 6
  • 2
    If you're looking at ASCII bytes in a string they are already each their own ASCII value. Look for a print single character system call or interrupt in your environment. If there isn't one then you can copy the character to memory and use a print multiple characters system call or interrupt, with a length argument of 1. – Erik Eidt Mar 02 '23 at 21:38
  • I'm sorry, I'm relatively new to x86 assembly and I do not understand what you explained. I was expecting something like python where you can do `ord(char)` to get the ascii and `chr(char)` to get the character. Is there no such thing in assembly? Thanks! – 1nc0gn170 Mar 02 '23 at 21:47
  • 1
    That's correct. Those operations take zero instructions. There are no types in assembly. Even in C, a `char` is just an integer type like `int` but smaller; you can add/subtract it, so again `ord` and `chr` don't need to exist. You can do `int foo = (unsigned char)my_char;` if you want to promote an ASCII character value to a wider type; the asm equivalent is `movzx eax, byte [mem]`. (C doesn't require ASCII; a C implementation could be EBCDIC for example. But in x86 assembly for mainstream OSes, you can assume ASCII or UTF-8, or an 8-bit charset like ISO-8859-1 that's ASCII-compatible) – Peter Cordes Mar 02 '23 at 21:51
  • Thank you for your answer @PeterCordes . Once I've read the character and stored it inside `al`, can I just write `al `using the `write` syscall and `stdout` to get the ASCII value then? – 1nc0gn170 Mar 02 '23 at 21:53
  • No. First, the `write` syscall needs the bytes to be written to be in memory, not a register. Second, I think you might be wanting to output a string (of ASCII characters) like `97` that is a decimal representation of the integer value of the byte (ASCII character) of the original string, such as `'a'`. In asm source those are two ways of writing the exact same integer constant. The C equivalent is `printf("%d\n", (int)str[i]);`, where the printf library function will generate the string in a buffer and `write` it to stdout. – Peter Cordes Mar 02 '23 at 21:57
  • 1
    To do that manually in asm, see [How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)](https://stackoverflow.com/a/46301894). If you just write the one byte with value `97` to stdout, your terminal will show that as `a`, because it's the ASCII code for that letter. If you want numbers made of multiple digits to represent that one byte's value, you have to run code which generates that string of digits and `write` that. Decimal and hex digit-strings are serialization formats for integers. – Peter Cordes Mar 02 '23 at 21:57
  • The reason `chr` and `ord` exist in languages like Python is that string vs. integer are separate types, and printing (or string-concat) functions figure out what to do based on type. In asm, doing something different with the same byte is up to you, the programmer, writing different instructions. – Peter Cordes Mar 02 '23 at 22:07
  • There's no such thing as ORD() and it isn't needed in assembly. – Erik Eidt Mar 02 '23 at 22:14

0 Answers0