[英]How to find string length in HLA?
我需要幫助查找用戶輸入的字符串長度。
樣本 output:
Feed Me: asdf
The String You Entered: asdf Has Length = 4
我該怎么做? 在互聯網上很難找到HLA 資源。 我得到的output是-23,應該是字符串的長度。 如果我將 pop (EBX) 更改為 pop (baseStringAddress),結果將打印第一個字符串的 ASCII 十進制數。
這是我的代碼,但我認為它不對。
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;
環境
筆記
例子
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.