Search code examples
assemblyhla

HLA Adding Up All The Numbers Between One And n


Assembly Language

Write a program that computes (n)(n+1)/2. It should read the value “n” from the user. Hint: you can compute this formula by adding up all the numbers between one and n.

I have a challenge in writing the above code in HLA. I have managed to get the following

program printing_n_Numbers;
    #include("stdlib.hhf");
    static
        n:int32;
        i:int32;
begin printing_n_Numbers;
    stdout.put("Enter n: ");
    stdin.get(n);
    mov(0,ecx)
    stdout.put("printing ",n," Numbers ",nl);
    for(mov(0,eax);eax<=n;add(1,eax)) do
        for(mov(0,ebx);ebx<eax;add(1,ebx)) do
            ecx = add(eax,ebx);
            stdout.put("N was = ");
            stdout.puti32(exc);
            stdout.newln();
        endfor;
    endfor;
end printing_n_Numbers;  

when i input a number like 6, the output is

Enter n: 6
printing 6 Numbers
N was = 1
N was = 2
N was = 3
N was = 4
N was = 5
N was = 2
N was = 4
N was = 6
N was = 3
N was = 6
N was = 4
N was = 8
N was = 5
N was = 6

How would i code it to output the sum of numbers enterd?


Solution

  • Solved

    After multiple changes the program worked. this is how i modified it

    mov(0,ecx);
        stdout.put("You Have Entered: ",n,nl);
        for(mov(0,eax);eax<=n;add(1,eax)) do
            add(eax,ecx);
        endfor;
    

    In order to print the sum, this is the code

    stdout.puti32(ecx); 
    

    I used stdout.puti32 to convert the hexadecimal to the original decimal number system