我试图找出为什么我的程序不输出 1 如果字符串以
A
。
例如,这就是它的输出
Please enter a string to process
sfrA
----> here is the string you entered: sfrA
Please enter a string to process
gtf
----> here is the string you entered: gtf
相反,它应该输出
Please enter a string to process
sfrA
after endsWithA --- result= 1
我没有看到我的代码的一部分在输出中工作,并且在输入 gtf 后我还收到内存访问冲突错误。非常感谢任何帮助。
这是我目前的代码
program StringProgram;
#include( "stdlib.hhf" );
#include( "cs17string.hla" ); // allows use of gets and puts
static
stringData : dword;
answer : int32;
procedure endsWithA( stringData : dword ); @nodisplay; @noframe;
static
dReturnAddress : dword;
begin endsWithA;
// Preserve registers
push( EAX );
push( EBX );
push( ECX );
push( EDX );
// Get the return address off the stack
pop( dReturnAddress );
// Get stringData off the stack
mov( stringData, EAX );
// Calculate the length of the string
mov( EAX, EBX );
mov( EAX, ECX );
// Check if the string is empty
cmp( ECX, 0 );
je no_end_with_a;
// Get the last character
dec( ECX );
add( EBX, ECX );
mov( [EBX], AL );
// Compare the last character with 'A' and 'a'
cmp( AL, 'A' );
je end_with_a;
cmp( AL, 'a' );
je end_with_a;
no_end_with_a:
mov( 0, EAX );
jmp done;
end_with_a:
mov( 1, EAX );
done:
// Restore the registers used
pop( EDX );
pop( ECX );
pop( EBX );
pop( EAX );
// Push back the return address
push( dReturnAddress );
// Return from function
ret();
end endsWithA;
begin StringProgram;
stdout.put( "Please enter a string to process", nl );
// This code allocates a string of size 80
mov( @size( int8 ), AL );
mov( 80, BL );
inc( BL );
mul( BL );
mov( 0, EBX );
mov( AX, BX );
malloc( EBX );
mov( EAX, stringData );
// Let's try reading a value into the string
mov( stringData, EAX );
push( EAX );
mov( 80, CX );
push( CX );
call gets;
// Print the string
stdout.put( "----> here is the string you entered: " );
mov( stringData, EAX );
push( EAX );
call puts;
stdout.newln();
// Initialize EAX before calling the function
mov( 0, EAX );
// Pass the string parameter to the function
mov( stringData, EAX );
call endsWithA;
mov( EAX, answer );
// Show the results
stdout.put( "after endsWithA --- result=" );
stdout.put( answer );
stdout.newln();
end StringProgram;
您必须使用
pop( stringData, EAX );
而不是 mov( stringData, EAX );
才能将 stringData 从堆栈中取出。另外,您必须使用 [] 和索引来获取 stringData 中的字符。
例如,
mov(0, CX);
mov(index, EDX);
myLoop:
mov([EBX+EDX], CL);
cmp(CL, CH);
je Done;
inc(EDX);
jmp myLoop;
Done:
此循环迭代 EBX 中保存的字符串中的字符,直到结束(达到 0,空字符)。 希望这有帮助!