【问题标题】:Print a string out by letter in assembler (NASM) [closed]在汇编程序(NASM)中按字母打印字符串[关闭]
【发布时间】:2013-12-27 21:25:53
【问题描述】:

(包括 MinGW)

把一个字符串作为一个整体打印出来很容易,但是我怎样才能改变这个代码来打印出每个字母:

global  _main     
extern  _printf  

section .const
  hello db 'Hello'
section .text

_main:
    push    hello     
    call    _printf
    add esp,4

【问题讨论】:

    标签: assembly nasm


    【解决方案1】:

    首先,您使用printf 错误! printf 有一个格式参数,printf docs 现在习惯于正确使用它,以后会减少错误/坏事。 Uncontrolled format string, How can a Format-String vulnerability be exploited?

    如果您阅读文档,它会向您显示显示字符而不是字符串的格式说明符,但是,您可以在不进行研究或阅读文档的情况下提出问题。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2017-03-31
      • 2015-09-17
      • 1970-01-01
      相关资源
      最近更新 更多