【问题标题】:Debug code regarding parsing a string character by character in NASM assembly for IA32关于在 IA32 的 NASM 程序集中逐个字符解析字符串的调试代码
【发布时间】:2016-03-18 15:08:06
【问题描述】:

我是汇编编程的新手。我偶然发现了一个程序,在该程序中我需要编写一个代码来从用户那里获取一个字符串和一个数字,并将字符串的每个字符增加给定的数字。

我做了以下事情:-

    section .bss
        s2 resb 20   ;output string
        s1 resb 20   ;input string
        num resb 2   ;input number
        count resb 1 ;length of the input string
    section .data

    section .text
        global _start
    _start:
        mov eax,3      ;taking input string from the user
        mov ebx,0
        mov ecx,s1
        mov edx,20
        int 0x80

        mov eax,3     ;taking input number from user
        mov ebx,0
        mov ecx,num
        mov edx,2
        int 0x80

        mov al,'1'     ;initializing count to 1
        sub al,'0'
        mov [count],al

        mov ecx,20     ;no of times the loop can execute
        mov esi,s1     ;to use movsb on s1 and s2
        mov edi,s2

        mov bl,[num]     ;converting string num to integer
        sub bl,'0'

        loop1:      ;parse the string character by character
        lodsb 
        cmp al,00   ;exit out when encounter end_of_file
        je _exit
        add al,bl
        stosb
        inc byte [count]    ;increament count for every possible character except end_of file
        loop loop1

    _exit:
        cld
        rep movsb
        mov edx,count
        mov ecx,s2
        mov ebx,1
        mov eax,4
        int 0x80

        mov eax,1
        int 0x80

当我运行代码时,它会产生预期的输出和一些乱码。 我无法理解我的代码的问题。

【问题讨论】:

    标签: linux debugging assembly x86 nasm


    【解决方案1】:

    接近尾声:

        mov edx,count
    

    这会使用count地址 加载edx 寄存器,类似于0x804912a。你不想写 0x804912a 字节。

    您希望edx 加载有count内容。请注意,count 是一个字节,但 edx 是一个 32 位寄存器,因此您需要对其进行零扩展。您可能想用

    替换该指令
        movzx edx, byte [count]
    

    更改后,您的程序按预期运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2015-10-06
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 2020-03-21
      相关资源
      最近更新 更多