[英]Debug code regarding parsing a string character by character in NASM assembly for IA32
I am a novice in assembly programming.I stumbled across a program in which i am required to write a code to take a string and a number from the user and increment each character of the string by the given number.我是汇编编程的新手。我偶然发现了一个程序,在该程序中,我需要编写一个代码来从用户那里获取一个字符串和一个数字,并将字符串的每个字符增加给定的数字。
I have done the following:-我做了以下事情:-
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
When i run the code,it produces the expected output and some gibberish characters.当我运行代码时,它会产生预期的输出和一些乱码。 I am not able to understand the problem with my code.我无法理解我的代码的问题。
Near the end:接近尾声:
mov edx,count
This loads the edx
register with the address of count
, which is something like 0x804912a.这将使用count
的地址加载edx
寄存器,该地址类似于 0x804912a。 You don't want to write 0x804912a bytes.您不想写入 0x804912a 字节。
You want edx
loaded with the contents of count
.您希望edx
加载count
的内容。 Note that count
is a byte but edx
is a 32-bit register, so you'll want to zero-extend it.请注意, count
是一个字节,而edx
是一个 32 位寄存器,因此您需要对其进行零扩展。 You probably want to replace that instruction with您可能想将该指令替换为
movzx edx, byte [count]
After the change, your program works as expected.更改后,您的程序将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.