x86中字符串和变量的串联

时间:2019-03-09 13:20:32

标签: x86 nasm x86-16

我正在尝试连接字符串和变量,并将其存储到x86中的新变量中。 我正在使用nasm编写汇编代码。 我想做的是这样的:

a = 1;
b = 2;
c = "values are: " + a + " and " + b ;
print c;

但是我不知道如何连接并为新变量赋值

1 个答案:

答案 0 :(得分:3)

a = 1;
b = 2;
c = "values are: " + a + " and " + b ;

此数据大致翻译为:

a db 1
b db 2
c db "values are: ? and ?$"

您的变量 a b 是数字。您需要先将它们转换为文本,然后再将它们插入字符串,在此简化示例中,该字符串使用问号字符()作为单个字符占位符。

mov al, [a]       ;AL becomes 1
add al, '0'       ;AL becomes "1"
mov [c + 12], al  ;12 is offset of the first '?'
mov al, [b]       ;AL becomes 2
add al, '0'       ;AL becomes "2"
mov [c + 18], al  ;18 is offset of the second '?'
mov dx, c         ;Address of the string
mov ah, 09h
int 21h           ;Print with DOS

这是上述代码的替代方法。它的说明短了一些,但缺点是不能太重用! (因为add取决于占位符保持为零)

a db 1
b db 2
c db "values are: 0 and 0$"


mov al, [a]       ;AL becomes 1
add [c + 12], al  ;12 is offset of the first '0'
mov al, [b]       ;AL becomes 2
add [c + 18], al  ;18 is offset of the second '0'
mov dx, c         ;Address of the string
mov ah, 09h
int 21h           ;Print with DOS