尝试显示字符串的代码中的错误

时间:2015-11-25 19:55:59

标签: linux string macros nasm 32-bit

我正在写一个 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

我该如何纠正?

1 个答案:

答案 0 :(得分: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