在 x86_64 程序集中将一个 .data 和一个 .bss 字符串连接在一起

Concatenate one .data and one .bss string together in x86_64 assembly

我正在写一个程序,它会询问你的名字,然后像这样打印出来:

<name> is your name?

这是我当前的代码:

section .data
    promptText: db "What is your name: ", 0
    promptTextLength: equ $ - promptText
    secondText: db " is your name?", 0

section .bss
    name resb 15

section .text:
    global _start

_start:

    ; Print prompt message
    mov rax, 1
    mov rdi, 1
    mov rsi, promptText
    mov rdx, promptTextLength
    syscall

    ; Get name
    mov rax, 0
    mov rdi, 0
    mov rsi, name
    mov rdx, 15
    syscall

    ; Somehow concatenate "name" and "secondText"

    ; Print second message
    mov rax, 1
    mov rdi, 1
    mov rsi, nameAndSecondText
    mov rdx, nameAndSecondTextLength
    syscall

    ; Exit 0
    mov rax, 60
    mov rdi, 0
    syscall

所以,几乎添加 namesecondText,然后将它们打印到终端。

然而,

我不想只调用 SYS_WRITE 两次,要打印两个字符串,我需要将 namesecondText 组合到它们自己的变量中。

谢谢!

P.S 对不起,如果我使用了错误的术语,我已经习惯了高级语言,例如 Python、C 等

您不需要字符串的终止 NUL,因为您不使用检查此字符的函数。

删除section .text:中的冒号。然后就可以用GDB调试程序了。

我建议直接将 secondText 附加到输入的名称。为此,您必须为变量 name 保留更多 space。 SYS_READ 没有注意到这一点,因此 space 在调用 SYS_READ 后保持空白。

section .data
    promptText:         db "What is your name: "
    promptTextLength:   equ $ - promptText
    secondText:         db " is your name?", 10
    secondTextLength:   equ $ - secondText

section .bss
    name resb 15 + secondTextLength ; Space for input and appended string

section .text
global _start

_start:

    ; Print prompt message
    mov rax, 1                  ; SYS_WRITE - http://man7.org/linux/man-pages/man2/write.2.html
    mov rdi, 1                  ; fd = STDOUT
    mov rsi, promptText         ; *buf
    mov rdx, promptTextLength   ; Count of bytes to write
    syscall                     ; Call Linux

    ; Get name
    mov rax, 0                  ; SYS_READ - http://man7.org/linux/man-pages/man2/read.2.html
    mov rdi, 0                  ; fd = STDIN
    mov rsi, name               ; *buf
    mov rdx, 15                 ; Max count of bytes to read
    syscall                     ; Call Linux - return EAX = number of bytes read
    dec rax                     ; number of bytes read without ENTER

    ; Append secondText
    mov rsi, secondText         ; *source
    mov rdi, name               ; *dest
    add rdi, rax                ; Set pointer one byte behind the real name
    mov rcx, secondTextLength   ; Count of bytes to copy
    lea rbx, [rax + rcx]        ; Save the total length of the string
    rep movsb                   ; Copy RCX bytes from [RSI] to [RDI]

    ; Print name (input + second message)
    mov rax, 1                  ; SYS_WRITE - http://man7.org/linux/man-pages/man2/write.2.html
    mov rdi, 1                  ; fd = STDOUT
    mov rsi, name               ; *buf
    mov rdx, rbx                ; Count of bytes to write (RBX was saved above)
    syscall                     ; Call Linux

    ; Exit (0)
    mov rax, 60                 ; SYS_EXIT
    mov rdi, 0                  ; Exitcode
    syscall                     ; Call Linux / no return