【问题标题】:Accepting and printing two strings in nasm assembly在 nasm 汇编中接受和打印两个字符串
【发布时间】:2013-08-26 15:02:24
【问题描述】:

我有这个代码:

section .data
   msg3 db 'Enter Two Strings: '
   msg3Len equ $ -msg3

section .bss
   string1Len resb 1
   string1 resb 0
   string2Len resb 1
   string2 resb 0

section .text
   global _start

_start:
   mov eax,4
   mov ebx,1
   mov ecx,msg3
   mov edx,msg3Len
   int 80h

   mov eax,3
   mov ebx,1
   mov ecx,string1
   int 80h

   dec al
   mov byte [string1Len], al

   mov eax,3
   mov ebx,1
   mov ecx,string2
   int 80h

   dec al
   mov byte [string2Len], al

   mov eax,4
   mov ebx,1
   mov ecx,string1
   mov edx,[string1Len]
   int 80h

   mov eax,4
   mov ebx,1
   mov ecx,string2
   mov edx,[string2Len]
   int 80h

   mov eax, 0
   mov ebx, 1
   int 80h

我在打印两个字符串时遇到问题。它打印多余和垃圾字符。此外,当我打印三个字符串时,它会打印出过多的字符。我的代码看起来正确时有什么问题?

【问题讨论】:

  • resb 0 保留零字节空间(即根本没有空间)。
  • 我应该改变什么?我将所有 resb 0 更改为 resb 1,它仍然打印垃圾字符。

标签: string assembly printing nasm


【解决方案1】:

当您从标准输入读取到string1string2 时,您正在写入尚未分配的内存(至少不是为此目的)。 resb 0 将保留零字节的空间(== 没有空间)。您应该保留与您希望阅读的最长字符串的大小一样多的空间。

另一个问题是您正在从 string1Lenstring2Len (mov edx,[string1Len]) 读取 32 位,即使这些变量的大小只有一个字节 - 这意味着您将读取超出实际的 3 个字节多变的。要么将变量设为双字(使用resd),要么使用movzx 指令将字节零扩展为双字(例如movzx edx,byte [string1Len])。

【讨论】:

    【解决方案2】:

    正如@Michael 所写的关于使用resdmovzx 的内容,这是一个更好的解决方法。

    我在 Ubuntu 64bit 上测试过:

    ; Compile with: nasm -f elf twoString.asm
    ; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 twoString.o -o twoString
    ; Run with: ./twoString
    
    SECTION .data
    msg     db      'Enter Two Strings: ', 0Ah
    msgLen equ $ - msg
    
    SECTION .bss
    string1:     resb    255
    string2:     resb    255
    
    SECTION .text
    global  _start
    
    _start:
    
        ;print msg
        mov     edx, msgLen
        mov     ecx, msg
        mov     ebx, 1
        mov     eax, 4
        int     80h
    
        mov     edx, 255        ; number of bytes to read
        mov     ecx, string1    ; reserved space to store our input (known as a buffer)
        mov     ebx, 0          ; write to the STDIN file
        mov     eax, 3          ; invoke SYS_READ (kernel opcode 3)
        int     80h
    
        mov     edx, 255        ; number of bytes to read
        mov     ecx, string2    ; reserved space to store our input (known as a buffer)
        mov     ebx, 0          ; write to the STDIN file
        mov     eax, 3          ; invoke SYS_READ (kernel opcode 3)
        int     80h
    
        mov     edx, 255
        mov     ecx, string1
        mov     ebx, 1
        mov     eax, 4
        int     80h
    
        mov     edx, 255
        mov     ecx, string2
        mov     ebx, 1
        mov     eax, 4
        int     80h
        mov     ebx, 0      ; return 0 status on exit - 'No Errors'
        mov     eax, 1      ; invoke SYS_EXIT (kernel opcode 1)
        int     80h
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多