使用NASM回文 [英] Palindrome using NASM
问题描述
我写了一个code寻找回文。但我的code显示输出不是pallindrome的所有案件。我的程序给出如下:
I wrote a code for finding palindrome. But my code shows output "is not pallindrome" for all the cases. My program is given following:
section .data
a db "mommom",0
b equ $-a
msg1 db "is pallindrome",10,0
msg2 db "is not pallindrome",10,0
msg3 db "",10,0
section .text
global main
extern printf
main:
nop
xor eax,eax
xor ebx,ebx
mov eax,a ;starting add
mov ebx,b
add eax,ebx
dec eax ;will use to indicate the last letter of a
xor ebx,ebx
xor edx,edx
xor ecx,ecx
start:
inc ecx
cmp ecx,(b/2) ;check will run for half of the word
jle check
jmp pal
check:
mov dl,byte[eax] ;last letter
cmp byte[a+ebx],dl ;frst letter compares with last letter
debug:
pusha ;debugging purpose.Used to catch the first letter of a
push byte[a+ebx]
push msg3
call printf
add esp,8
popa
checkContinue:
inc ebx ;use for check segment
dec eax
je start
jne nonPal
pal:
pusha
push msg1
call printf
add esp,4
popa
jmp done
nonPal:
pusha
push msg2
call printf
add esp,4
popa
jmp done
done:
nop
安托万马特仕已经给了我们上面的code指出的错误的相应版本都发生在这个code。他的话一节对我们来说是一样的新手非常重要的。在这里,在这上面的程序中,我试图打印居住在EBX寄存器的每一个字符,我没能得到。我会很感激,如果任何导师可以解决这个问题的这一部分。这会帮助我学习如何从一个字符串取每个字符。
Antoine Mathys has already given us the appropriate version of the above code pointing out the mistakes have occurred in this code. His remarks section is very important for us like newbie. Here, in this above program, I tried to print every character residing in the ebx register and I failed to get that. I will be grateful if any mentor can approach this portion of the problem. It will help me to learn how to take each character from a string.
推荐答案
下面是:
BITS 32
section .data
string db "mommom"
length equ $ - string
msg1 db "is pallindrome",0
msg2 db "is not pallindrome",0
section .text
global main
extern puts
main:
mov ebx, string ; start of word
mov eax, (string + length - 1) ; end of word
mov ecx, (length / 2) ; check will run for half of the word
check:
mov dl, [ebx] ; compare first and last letters
cmp [eax], dl
jne failure
inc ebx
dec eax
loop check
;; success
push msg1
call puts
add esp,4
jmp done
failure:
push msg2
call puts
add esp,4
done:
ret
一些言论:
- 请不要增加一个零,以测试这个词。这将使非回文。
- 您$ C $ C充满了不必要的指令(PUSHA /波帕,NOP,结算EDX但是只能使用DL,...)。尽量保持code尽可能简单。
- 请不要使用XOR招,写清楚
- 使用有意义的符号名称
- 以汇编算术EX pressions的优势
这篇关于使用NASM回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!