PROGRAM 8: strlen Function Write an HLA Assembly language program that implement
ID: 3913767 • Letter: P
Question
PROGRAM 8: strlen Function
Write an HLA Assembly language program that implements the following function:
procedure strlen( baseStringAddress: dword ); @nodisplay; @noframe;
This function should return in AL the number of characters in the string parameter, passed by its base address. Here is a sample program dialogue:
Feed Me: asdfasdf
The String You Entered: asdfasdf Has Length = 8
Feed Me: asdf
The String You Entered: asdf Has Length = 4
PROGRAM 9: Swapper
Write an HLA Assembly language program that implements a function which correctly sorts three parameters and returns a boolean value in AL which should be set to true if any swaps were performed to sort the sequence in increasing order. This function should have the following signature:
procedure swapper( var x : int16; var y : int16; var z : int16 ); @nodisplay; @noframe;
After invoking the function, the value of x <= the value of y <= the value of z. Your function must use reference parameters, changing the callers version of the variables passed to the function. Here is a sample program dialogue:
Gimme X: 12
Gimme Y: 1
Gimme Z: 50
After sorting, X = 1, Y = 12, Z = 50
Gimme X: 100
Gimme Y: 10
Gimme Z: 2
After sorting, X = 2, Y = 10, Z = 100
HINT: Even though the parameters are listed as int16, HLA expects you will pass them as memory addresses, requiring a full 32-bit value. So you'll have to use extended registers to hold the parameters, not 16-bit registers!
Explanation / Answer
strlen.hla
program main;
#include( "stdlib.hhf" );
#include( "cs17string.hla" );
const
nullchar : byte := 0;
static
//string to build on
stringData : byte[81] := [ 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c','a', 'b', 'c', 'a', 'b', 'c', 'c', 'a', 'b', 'c','0' ];
stringDataLen : uns16 := 80; // max number of chars
procedure strlen( baseStringAddress: dword ); @nodisplay; @noframe;
static
returnAddress : dword;
counter : int8 := 0;
tempchar: byte;
register1: dword;
begin strlen;
//entry sequence preserve EDX
mov(EDX, register1);
pop(returnAddress);
pop(baseStringAddress);
push(returnAddress);
push(register1);
//while loop to count characters
mov(baseStringAddress, EAX);
mov(0, DH);
WhileLp:
WhileLpTermination:
cmp([EAX],DH);
je WhileLpDone;
WhileLpBody:
inc(counter);
inc(EAX);
jmp WhileLp;
WhileLpDone:
mov(counter, AL);
//exit sequence
pop(EDX);
ret();
end strlen;
begin main;
//build string
stdout.put("Feed Me: ");
mov(&stringData, EAX);
push(EAX);
push(stringDataLen);
call gets;
mov(&stringData, EAX);
push(EAX);
call strlen;
//print string and result
stdout.put("The String You Entered: ");
mov(&stringData, EBX);
push(EBX);
call puts;
stdout.put(" Has Length = ");
stdout.puti8(AL);
stdout.newln();
end main;
swapper.hla
program main;
#include( "stdlib.hhf" );
static
one : int16;
two : int16;
three : int16;
procedure swapper( var x : int16; var y : int16; var z : int16 ); @nodisplay; @noframe;
static
returnAddress: dword;
register1: dword;
register2: dword;
register3: dword;
register4: dword;
temp: int16;
begin swapper;
//entry sequence preserve all registers
mov(EBX, register1);
mov(ECX, register2);
mov(EDX, register3);
mov(EAX, register4);
pop(returnAddress);
pop(EDX);
pop(ECX);
pop(EBX);
push(returnAddress);
push(register4);
push(register3);
push(register2);
push(register1);
//implementation of a bubble sort type algorithm
mov([EBX], EAX);
mov(AX, temp);
mov([ECX], EAX);
cmp(temp, AX);
jg swapXY;
mov([ECX], EAX);
mov(AX, temp);
mov([EDX], EAX);
cmp(temp, AX);
jg swapYZ;
jmp endsequence;
swapXY:
mov([EBX], EAX);
mov(AX, temp);
mov([ECX], EAX);
mov(AX, [EBX]);
mov(temp, AX);
mov(AX, [ECX]);
mov([ECX], EAX);
mov(AX, temp);
mov([EDX], EAX);
cmp(temp, AX);
jl endsequence;
mov([ECX], EAX);
mov(AX, temp);
mov([EDX], EAX);
mov(AX, [ECX]);
mov(temp, AX);
mov(AX, [EDX]);
mov([EBX], EAX);
mov(AX, temp);
mov([ECX], EAX);
cmp(temp, AX);
jl endsequence;
mov([EBX], EAX);
mov(AX, temp);
mov([ECX], EAX);
mov(AX, [EBX]);
mov(temp, AX);
mov(AX, [ECX]);
jmp endsequence;
swapYZ:
stdout.put("inYZ");
mov([ECX], EAX);
mov(AX, temp);
mov([EDX], EAX);
mov(AX, [ECX]);
mov(temp, AX);
mov(AX, [EDX]);
mov([EBX], EAX);
mov(AX, temp);
mov([ECX], EAX);
cmp(temp, AX);
jl endsequence;
mov([EBX], EAX);
mov(AX, temp);
mov([ECX], EAX);
mov(AX, [EBX]);
mov(temp, AX);
mov(AX, [ECX]);
endsequence:
pop(EBX);
pop(ECX);
pop(EDX);
pop(EAX);
ret();
end swapper;
begin main;
stdout.put("Gimme X: ");
stdin.get(one);
stdout.put("Gimme Y: ");
stdin.get(two);
stdout.put("Gimme Z: ");
stdin.get(three);
mov(&one, EAX);
push(EAX);
mov(&two, EAX);
push(EAX);
mov(&three, EAX);
push(EAX);
call swapper;
stdout.put("After sorting, X = ", one,", Y = ", two,", Z = ", three);
stdout.newln();
end main;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.