【问题标题】:Concatenate one .data and one .bss string together in x86_64 assembly在 x86_64 程序集中将一个 .data 和一个 .bss 字符串连接在一起
【发布时间】:2018-12-06 08:12:13
【问题描述】:

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

<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 等。

【问题讨论】:

    标签: assembly 64-bit nasm x86-64


    【解决方案1】:

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

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

    我建议将secondText 直接附加到输入的名称中。为此,您必须为变量name 保留更多空间。 SYS_READ 没有注意到这一点,因此在调用 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
    

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 2021-09-01
      • 2014-03-15
      • 2016-10-19
      • 1970-01-01
      • 2011-06-17
      相关资源
      最近更新 更多