Reputation: 1
I need help finding the string length of the user input.
sample output:
Feed Me: asdf
The String You Entered: asdf Has Length = 4
How do I do it? It's really hard to find HLA resources on the internet. The output that I got is -23 and it is supposed to be the length of the string. And if I change the pop (EBX) to pop (baseStringAddress) the result will print the ASCII decimal of the first string.
Here is my code but I don't think it's right.
program strlenFunction;
#include( "stdlib.hhf" );
#include( "cs17string.hla" );
const
nullchar: byte := 0;
static
iSize: int8:= 0; //array size
strData : byte[32];
strLength : uns16 := 31; // max number of chars
dArrayBaseAddress : dword := 0; // holds base address
procedure strlen( baseStringAddress: dword ); @nodisplay; @noframe;
static
return: dword;
arraySize: int16;
iEBX : dword := 0;
iEDX : dword := 0;
iECX : dword := 0;
begin strlen;
mov(EBX, iEBX);
mov(EDX, iEDX);
mov(ECX, iECX);
pop(return);
pop(EBX);
push(return);
push(iECX);//i
push(iEDX); //counter
push(iEBX);
mov(baseStringAddress, EBX);
mov([EBX], AL);
mov(baseStringAddress, ECX);
WhileStart:
mov(0, AH);
cmp([ECX], AH);
jne WhileInc;
je EndSequence;
WhileInc:
inc(ECX);
inc(EDX);
jmp EndSequence;
EndSequence:
stdout.puti8(AL);
pop(EDX);
pop(ECX);
pop(EBX);
ret();
end strlen;
begin strlenFunction;
stdout.put("Feed me: ");
mov(&strData, EAX);
push(EAX);
push(strLength);
call gets;
mov( @size( int8 ), AL );
mov( iSize, BL );
mul( BL );
mov( 0, EBX );
mov( AX, BX );
malloc( EBX );
mov( EAX, dArrayBaseAddress );
stdout.put("The String You Entered: ");
mov(&strData, EAX);
push(EAX);
call puts;
stdout.put(" Has Length = ");
mov(&strData, EAX);
push(EAX);
call strlen;
free( dArrayBaseAddress );
end strlenFunction;
Upvotes: 0
Views: 344
Reputation: 467
ENVIRONMENT
NOTE
EXAMPLE
program Strlen;
#include("stdlib.hhf")
storage
inputStr: string;
static
inputLen: int32;
begin Strlen;
// Prompt user for input
stdout.put("Feed me: ");
// Flush Input
stdin.flushInput();
// Allocate and read input
stdin.a_gets();
// Save result in inputStr
mov(EAX, inputStr);
// Display input back to user
stdout.put("The String You Entered: ", inputStr, nl);
// Calculate length of input string
str.length(inputStr);
// Save result in inputLen
mov(EAX, inputLen);
// Display input length to the user
stdout.put("Has Length = ", inputLen, nl);
// Free allocated memory
str.free(inputStr);
end Strlen;
Upvotes: 0