【问题标题】:Concatenation of strings and variables in x86x86 中字符串和变量的连接
【发布时间】:2019-03-09 13:20:31
【问题描述】:

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

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

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

【问题讨论】:

    标签: x86 nasm x86-16


    【解决方案1】:
    a = 1;
    b = 2;
    c = "values are: " + a + " and " + b ;
    

    这个数据大致翻译为:

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

    您的变量 ab 是数字。在将它们插入字符串之前,您需要将它们转换为文本,在这个简化的示例中,字符串使用问号字符 (?) 作为单个字符占位符。

    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
    

    【讨论】:

    • 如果我将其更改为适当的寄存器,相同的代码是否适用于 0x80 中断?
    • @lunatic955: Linux sys_write 需要一个指针+长度,而不是$ 终止符,但是将字节添加到占位符字符串中的基本逻辑显然是通用的。
    • 好的,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2016-11-15
    • 2018-06-23
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多