2012-11-16 58 views
1

我想知道我的问题在这里。它向后打印字符串,并且将正确的字符读入子元素来计算元音,但是如何将字符与顶部的字符进行比较存在问题。比较两个字符的麻烦

基本上,我如何在程序的开头定义字符,以便我可以正确比较它们?

//trying to make FMT_CHR2 work 

PROMPT: 
.ascii "Enter the lowercase string to evaluate: \0" 
FMT_STR: 
.ascii "%s\0" 
FMT_INT: 
.ascii "%d\0" 
FMT_CHR: 
.ascii "%c\0" 
FMT_CHR2: 
.ascii "\n FMTCHR2 %c\n\0" 
ENDTEST: 
.ascii "\0" 

A: .ascii "a" 
E: .ascii "e" 
I: .ascii "i" 
O: .ascii "o" 
U: .ascii "u" 
TEST: .ascii "TEST\n\0" 
RESULT: .ascii "\n Your string has %d non-blank characters\0" 



.section .data 
county: .long 0 

vowelCounter: .long 0 

.globl _main 

.section .text 


_main: 
    pushl %ebp    # save old frame ptr 
movl %esp,%ebp   # set new frame ptr & save local var space 

//create local variable space 
subl $100,%esp 

pushl $PROMPT 
call _printf 
subl $4,%esp 

leal -4(%esp),%ebx 
pushl %ebx 
call _gets 
subl $4,%esp 

pushl (%ebx) 
call _rprint 
subl $4,%esp 

pushl county 
pushl $RESULT 
call _printf 
subl $4,%esp 

pushl vowelCounter 
pushl $FMT_INT 
call _printf 
subl $4,%esp 

leave 
ret 

_rprint: 

pushl %ebp 
movl %esp,%ebp 

cmpb $0,(%ebx) 
je ending 

call _vowelcount 

pushl (%ebx) 
addl $1,%ebx 
call _rprint 

pushl $FMT_CHR 
call _printf 
subl $4,(%esp) 

incl county 

ending: leave 
ret 

_vowelcount: 

push %ebp 
movl %esp,%ebp 

movl (%ebx),%ecx 
pushl %ecx 
push $FMT_CHR2 
call _printf 
cmpl %ecx,A 
je _vAdd 
cmpl %ecx,E 
je _vAdd 
cmpl %ecx,I 
je _vAdd 
cmpl %ecx,O 
je _vAdd 
cmpl %ecx,U 
je _vAdd 

jmp end2 

_vAdd: 
incl vowelCounter 

end2: leave 
ret 

回答

0

字符存储为单个字节。你应该比较它们,例如:cmpb %cl, A应该工作,或者直接cmpb %cl, 'A'。一个建议:你可以把它们当作一个数组,而不是单独测试每个数组。

另外请注意,从您应该添加到堆栈指针堆栈清理函数参数,例如addl $4, %esp而不是subl $4, %esp,绝对不是subl $4, (%esp)

+0

好吧,我都这样了,它是说,pushl线有一个无效的操作数。 \t pushl%ebp的 \t MOVL%ESP,%EB​​P \t MOVB(%EBX),%CL \t pushb%CL \t推$ FMT_CHR2 \t呼叫_printf \t CMPB%CL,A –

+0

当然它说的'pushb'是无效的,而不是'pushl'。你只能将32位压入栈中,'printf'将知道使用低8位(也就是说,如果你提供了正确的格式),或者你可以明确地将其零或符号扩展。还要注意'printf'可以改变'ecx',所以你不能在printf返回后指望它包含字符。 – Jester