簡體   English   中英

如何在 HLA 中查找字符串長度?

[英]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;

環境

  • HLA(高級匯編程序 - HLABE 后端,LD 鏈接器)版本 2.16 build 4409(原型)
  • Ubuntu 21.04

筆記

  • HLA 提供了更高級別的功能,有助於在過渡到較低級別的裝配概念之前布局和測試所需的功能。 以下示例提供了一個使用 HLA 高級功能的工作解決方案,它為組裝工作提供了一個良好的起點。
  • 與高級語言相比,匯編代碼的可讀性平均較低,因此最好在整個代碼中頻繁使用注釋。
  • HLA 還允許縮進,這進一步提高了代碼的可讀性。

例子

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM