-1

This has been asked before but I can't seem to figure it out: how do I print an ascii value in assembly x86 32 bit.

mov eax, 10
add eax, 48
;print contents of eax
Tom Unsworth
  • 17
  • 1
  • 7
  • 1
    Show your attempt and specify what was causing you problem. See the [canonical answers in the x86 tag wiki](https://stackoverflow.com/tags/x86/info) too. – Jester Dec 17 '19 at 17:43
  • Under what OS? You can convert an integer to a string of ASCII digits with [How do I print an integer in Assembly Level Programming without printf from the c library?](//stackoverflow.com/a/46301894) which also shows using a Linux system call to write that string to stdout. Obviously under any other OS you can do whatever you want with the string of digits. – Peter Cordes Dec 17 '19 at 17:50
  • im using ubuntu 18, its very simple yet i'm struggling to get it to output – Tom Unsworth Dec 17 '19 at 18:09
  • You either link against the libc and use the `printf` function or you write your own code that translates the integer into a string digit by digit and then prints the string. – fuz Dec 17 '19 at 18:39
  • okay I want to link against the libc and use the printf function. how would i go about doing this? could you point me in the right direction – Tom Unsworth Dec 17 '19 at 18:51
  • 1
    See the duplicate. If this does not answer your question, please let me know. – fuz Dec 18 '19 at 01:24

2 Answers2

1

Here's how to do it using printf on 32-bit Linux:

~/tmp: cat t.s
    .intel_syntax noprefix
    .global main
main:
    mov eax, 10
    add eax, 48
    push eax
    push offset .L1
    call printf
    add esp, 8
    xor eax, eax
    ret
.L1: .asciz "%d\n"
~/tmp: gcc -m32 t.s
~/tmp: a.out
58
~/tmp:

Here's how to do it on 64-bit Linux:

~/tmp: cat t.s
    .intel_syntax noprefix
    .global main
main:
    sub rsp, 8
    mov eax, 10
    add eax, 48
    lea rdi, .L1[rip]
    mov esi, eax
    xor eax, eax
    call printf
    add rsp, 8
    xor eax, eax
    ret
.L1: .asciz "%d\n"
~/tmp: gcc t.s
~/tmp: a.out
58
~/tmp:
prl
  • 11,716
  • 2
  • 13
  • 31
0

It depends on how you want to print. You can...

Link against a print routine in a library. Use bios interrupt 0x10 to print to the screen. Print using a serial port (if you are doing low level coding).

Brian Makin
  • 887
  • 1
  • 6
  • 17
  • how would I use bios interrupt 0x10 to print to the screen? – Tom Unsworth Dec 17 '19 at 17:40
  • First you convert the integer into a string in memory. use int 0x10 to jump to the bios interrupt handler. register ah contains the function code of the operation you want to perform. Depending on the cpu model (you are likely good). You can use function code 0x13 in ah to print a string to the screen. – Brian Makin Dec 17 '19 at 17:45
  • @TomUnsworth: You can't use `int 0x10` in 32-bit mode. This part of the answer isn't relevant to your question. There is no OS-independent way to print. Either you use a system call or (on bare metal) write it to a device yourself, e.g. VGA memory. – Peter Cordes Dec 17 '19 at 17:48