Reputation: 1
first of i just wanna say im very new to coding and trying to learn. I have to write a HLA code for a program that lets u enter numbers. after each number is entered it gives u a new line to enter another number, until "0" is entered. then the program should stop and show u in all the numbers u entered in the correct order. I need to use the HEAP memory to store the numbers in. so when they come out they are in the wrong order. this is where i am right now
program Project3;
#include("stdlib.hhf")
static
count: int32 := 0;
mempointer: int32;
begin Project3;
mem.alloc(1000);
mov(eax, mempointer);
loopStart:
stdout.put("Enter a number, 0 to stop: ");
stdin.flushInput();
stdin.geti32();
mov(eax, ebx);
cmp(ebx, 0);
je exitLoop;
add(1, count);
mov(count, ecx);
shl(2, ecx);
mov(mempointer, eax); add(ecx, eax);
mov([eax], ebx);
jmp loopStart;
exitLoop:
stdout.put(nl, "Entered numbers: ");
while (count <> 0) do
sub(1, count);
mov(count, ecx);
shl(2, ecx);
mov(mempointer, eax); add(ecx, eax);
mov(ebx, [eax]);
stdout.puti32(ebx);
stdout.put(" ");
endwhile;
mem.free(mempointer);
end Project3;
As it is it compiles and run but it only displays a 0 for each number I've entered. does anyone know how to fix that?
ive tried using manny diffrent ways of storing the numbers so they come out in the order ive entered them. but cant find what im doing wrong.
Upvotes: -1
Views: 99
Reputation: 5805
Use a paper computer to check your algorithm. Draw an empty box for each important memory-variable and register, walk through the code line by line and update the affected box with new value.
Whenever you encounter stdin.geti32();
, ask your schoolmate to draw two or three random numbers, for instance 7, 3, 5, 0. After 0 you are jumping to exitLoop:
and you should end up with something like
---------
eax |mptr+12|
---------
-----
ebx | 5 |
-----
------
ecx | 12 |
------
-----
counter | 3 |
-----
---------------------------
mptr* | ? | 7 | 3 | 5 | ? | ? |...
---------------------------
0 4 8 12 16 20
Now it should be obvious what went wrong. You are incrementing counter
too early.
Also to display back the array in the right order, you may need another memory variable such as countOut: int32 := 0;
which you will increment after the number has been displayed, and check whether counterOut
equals counter
.
Upvotes: 1