-1

(Includes MinGW)

It's easy to print out a string as a whole, but how can i change this code to print out each letter:

global  _main     
extern  _printf  

section .const
  hello db 'Hello'
section .text

_main:
    push    hello     
    call    _printf
    add esp,4
user3140640
  • 3
  • 1
  • 3

1 Answers1

0

First off, you are using printf wrong! printf has a format parameter, printf docs get used to using it the correct way now, and it will cut down on bugs/bad things later. Uncontrolled format string, How can a Format-String vulnerability be exploited?

If you would read the docs, it would show you the format specifier to display characters instead of strings, but hey, one can ask a question without doing research or reading docs.

global  main     
extern  printf, exit

section .data
hello       db 'Hello', 0
hello_Len   equ $ - hello

fmtstr      db  "%s", 10, 0
fmtchr      db  "%c", 10, 0

section .text
main:
    push    hello  
    push    fmtstr   
    call    printf
    add     esp, 4 * 2

    xor     ebx, ebx
    mov     esi, hello
PrintHello:
    movzx   eax, byte [esi + ebx]
    push    eax
    push    fmtchr
    call    printf
    add     esp, 4 * 2
    inc     ebx
    cmp     ebx, hello_Len - 1
    jne     PrintHello

    push    0
    call    exit
Community
  • 1
  • 1
Gunner
  • 5,780
  • 2
  • 25
  • 40