-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.

trincot
  • 317,000
  • 35
  • 244
  • 286
trolldeg
  • 1
  • 1
  • When you have calculated the number address into `eax`, you should load that number into `ebx` and then `stdout.puti32(ebx);`, but you are loading the number with `mov(ebx, [eax]);` which seems to have operands swapped. Try `mov([eax],ebx);`. – vitsoft Apr 28 '23 at 19:17
  • @vitsoft I tried changing it to `mov([eax], ebx);` but it still displays 0 for each number entered instead of the actual number, can think of something else that might cause this? – trolldeg Apr 28 '23 at 19:29
  • I noticed a similar mistake in the first loop: `mov([eax], ebx);` `jmp loopStart;`. When you store the input number from `ebx` into array addressed with `eax`, I think it should be `mov(ebx,[eax]);`. – vitsoft Apr 28 '23 at 22:15
  • @vitsoft Thank you! it helped, to actually display the numbers I entered except that it dont print out the first number and they come out in the wrong order. (https://ibb.co/nM9FJk6) – trolldeg Apr 29 '23 at 00:23

1 Answers1

1

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.

vitsoft
  • 5,515
  • 1
  • 18
  • 31
  • 1
    You don't need another *memory* variable; a counter in a register would be fine. You can't compare two memory variables against each other with a single instruction so it's less convenient to use them. – Peter Cordes Apr 29 '23 at 08:18