【问题标题】:Errors in code that tries to display strings尝试显示字符串的代码错误
【发布时间】:2016-02-28 18:20:06
【问题描述】:

我正在编写一个 nasm 程序,它使用预处理器的指令和宏简单地打印一个字符串。代码如下:

%define hello "Hello, world!"
%strlen size_h hello

%macro print 2
  mov eax, 4
  mov ebx, 1
  mov ecx, %1
  mov edx, %2
  int 80h
%endmacro

section .text
global _start

_start:
  print hello, size_h
  mov eax, 1
  mov ebx, 0
  int 80h ;exit

我正在使用 ld 链接器。

它向我显示了两个警告:

character constant too long
dword data exceeds bounds

我该如何纠正这个问题?

【问题讨论】:

    标签: linux string macros nasm 32-bit


    【解决方案1】:

    宏只是替换字符串。所以,print hello, size_h 会变成

    mov eax, 4
    mov ebx, 1
    mov ecx, "Hello World!"
    mov edx, 13
    int 80h
    

    您看,您尝试使用字符串加载ECX,因为Int 80h/EAX=4 需要一个地址。首先,您必须存储字符串,然后您可以加载 ECX 及其地址。 NASM 不会为您这样做。

    以下宏将文字存储在 .text 部分中(您无法在此处更改):

    %macro print 2
        jmp short %%SkipData
        %%string: db %1
        %%SkipData:
        mov eax, 4
        mov ebx, 1
        mov ecx, %%string
        mov edx, %2
        int 80h
    %endmacro
    

    此宏切换到.data 部分并返回到.text

    %macro print 2
        section .data
        %%string: db %1
        section .text
        mov eax, 4
        mov ebx, 1
        mov ecx, %%string
        mov edx, %2
        int 80h
    %endmacro
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多