0

So the program I'm trying to write in HLA should do the dollowing: I enter a digit, and it gives a pattern of numbers. The pattern should show all the odd numbers from 1 up to that number followed by all the even numbers from 2 up to that number. Here is my code:

program boxit; 
#include ("stdlib.hhf"); 
static iDatavalue : int8 := 0 ; 


Begin boxit; 

    stdout.put("Gimme a decimal value to use as n: "); 

    stdin.get(iDatavalue); 
    mov(iDatavalue, BH); 
DoWhileLp:
DoWhileLpBody:
ForLp:
InitializeForLp:
    mov(BH, CH); 
ForLpTerminationTest:
    cmp(CH, 0); 
    jnl ForLpDone; 
ForLpBody:
    stdout.put("I = ", CH, nl); 
ForLpIncrement:
    dec(CH); 
    jmp ForLpTerminationTest; 
ForLpDone:
    dec(CH); 
DoWhileLpTermination:
    cmp(CH, 0); 
    jbe DoWhileLpDone; 
    jmp DoWhileLpBody; 
DoWhileLpDone:
    stdout.puti8(BH); 

end boxit;

However, it is a infinite loop and I'm not sure how to solve this issue.

I Highly appreciate any and all help!

Waqar
  • 8,558
  • 4
  • 35
  • 43
  • 1
    I can't make sense of your code at all. I don't see how it even starts with `1` let alone why it has a for loop and a do-while loop. – Jester Jul 17 '20 at 22:54
  • 3
    You need a C version of this so you can workout an algorithm -- developing an algorithm from scratch in assembly is very hard when you are first learning it. This code has two nested loops both using the same loop control variable, which is not good, so yes it looks like an infinite loop. For your assignment, you don't need nested loops but two sequential loops would work. This code is using count down, but your assignment tells you to output the numbers counting upwards. – Erik Eidt Jul 18 '20 at 00:26

1 Answers1

1

ENVIRONMENT

  • HLA (High Level Assembler - HLABE back end, LD linker) Version 2.16 build 4409 (prototype)
  • Ubuntu 20.04.1

NOTE

  • As mentioned in previous comments:
    • Nested loops using the same loop control variable is poor design and leads to hard to debug problems such as an infinite loop.
    • This assignment is more simply solved using two sequential loops instead of nested loops.
    • Starting with a higher level language to design the algorithm is good practice. The following example provides a working solution using the higher level functionality of HLA which is a good starting point to work towards assembly.

EXAMPLE

program Boxit;
#include ("stdlib.hhf");

begin Boxit;
    stdout.put("Gimme a decimal value to use as n: ");
    stdin.geti32();

    for ( xor(EBX, EBX); EBX <= EAX; add(2, EBX) ) do
        stdout.puti32(EBX);
        stdout.put(", ");
    endfor;

    stdout.newln();

    for ( mov(1, EBX); EBX <= EAX; add(2, EBX) ) do
        stdout.puti32(EBX);
        stdout.put(", ");
    endfor;

end Boxit;
Kuma
  • 427
  • 5
  • 17