汇编程序x64 Nasm单字符串连接

时间:2016-09-18 19:09:38

标签: assembly nasm

我是汇编程序的新手,并尝试执行一些简单的任务,比如输入名称并获取'嘿名字'回答。 Sofar我读取输入并将其分配给我的未声明变量,而不是将此变量放入rdx寄存器并在显示器上显示。问题是我不知道如何将tekst2变量放到rdx而不是替换tekst;

  section .text

    section .data
tekst db "Hey ", 0ah

global _start
_start:

;read input
mov rax,0 ;numer funkcji sys_read
mov rdi,0
mov rsi,tekst2
mov rdx, 20
syscall
;move input to rbx for later compare
mov rbx,tekst
mov rbx,tekst2

;print
mov rax, 1 
mov rdi, 1 
mov rsi, rbx
mov rdx, 20
syscall
mov rax, 60
syscall

section .bss
tekst2: resw 1

1 个答案:

答案 0 :(得分:0)

假设这是一个赋值,范围是显示Hey "entry",这个剪切格式输出在命令提示符下面有一行,而在下一个上面有另一行。

            section .data
  Prompt:   db  10, 9, 'Hey '    
    Ends:   db  10

            section .bss
   Entry:   resb    80

            section .text
    global  _start

   _start:  mov     eax, 1                  ; SYS_WRITE
            mov     edi, eax                ; STDOUT
            mov     esi, Prompt
            mov     edx, Ends - Prompt
            push    rax                     ; We'll need these later to display
            push    rax                     ; another line feed after entry
            syscall             
            push    rsi                     ; Points to Ends, needed later

    ; Get input

            mov      al, 0
            mov     edi, eax
            mov     esi, Entry
            mov      dl, 80
            syscall

    ; Finally display string @ Ends

            pop     rsi                 ; Points to Ends
            pop     rdi                 ; STDOUT
            pop     rax                 ; SYS_WRITE
            mov      dl, 1
            syscall

            xor     rdi, rdi            ; Return code from prologue
            mov     eax, 60
            syscall

从技术上讲,字符串是串联的,只是它在视频内存中完成。

另一个版本是你可以在Entry之后立即声明Prompt来进行隐式连接,

          section .data
  Prompt:   db  10, 9, 'Hey '    
   Entry:   times 80 db 10

然后输入后,只需将6添加到已经在AL中的值,将其移至DL并显示提示&作为一个输入,实现与前一个示例相同的结果。您需要的一切都是附加代码,所需要的只是删除5行,移动5行并更改3个参数,.data中的新声明将起作用。

相关问题