Linux 尝试显示字符串的代码中出现错误
我正在编写一个nasm程序,它使用预处理器的指令和宏简单地打印字符串。代码如下:Linux 尝试显示字符串的代码中出现错误,linux,string,macros,nasm,32-bit,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 ea
%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
如何更正此问题?宏只是替换字符串。所以,
print hello,size\u h
将变成
mov eax, 4
mov ebx, 1
mov ecx, "Hello World!"
mov edx, 13
int 80h
你看,你试图用一个字符串加载ECX
,因为int80h/EAX=4
需要一个地址。首先必须存储字符串,然后才能加载带有地址的ECX
。NASM不会为你这么做的
以下宏将文本存储在.text
部分(您不能在此处更改):
此宏切换到.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
%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