【问题标题】:trouble with comparing two characters比较两个字符的麻烦
【发布时间】:2012-11-05 05:17:23
【问题描述】:

我想知道我的问题是什么。它向后打印字符串,并在计算元音的子程序中读入正确的字符,但是在将字符与顶部字符进行比较时存在问题。

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

//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

【问题讨论】:

    标签: string assembly ascii


    【解决方案1】:

    字符存储为单个字节。您应该像这样比较它们,例如:cmpb %cl, A 应该可以工作,或者直接cmpb %cl, 'A'。一个建议:您可以将它们视为一个数组,然后遍历它们,而不是单独测试每个。

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

    【讨论】:

    • 好的,我有这个,它是说 pushl 行的操作数无效。 pushl %ebp movl %esp,%ebp movb (%ebx),%cl pushb %cl push $FMT_CHR2 call _printf cmpb %cl,A
    • 肯定是说pushb 无效,而不是pushl。您只能将 32 位压入堆栈,printf 将知道使用低 8 位(也就是说,如果您提供正确的格式),或者您可以显式地将其置零或符号扩展。另请注意printf 允许更改ecx,因此您不能指望它包含printf 返回后的字符。
    猜你喜欢
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2011-01-18
    • 2020-02-13
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 2013-12-10
    相关资源
    最近更新 更多