-2

My program is stuck in an infinite loop when I try to print out the'#'s. How it should work is the user will input 2, 5, 8 and it should print:

Each # represents 1
##
#####
########

But if the max value is over 80, and for example, the input was 82, 10, 2 the output would be:

 Each # represents 2
    #########################################
    #####
    #

This is the broken up portion:

include pcmac.inc
        .MODEL  SMALL
        .586
        .STACK  100h
        .DATA
Val     DW 6
Count   DW 3
Array     DW 2, 3, 6
DisplayCH DB 13, 10, 'Each # respresents ' , '$'
        
        .CODE
        extrn GetDec:near
        extrn PutDec:near
PrintChar Proc
        _begin
        mov ax, @data
        mov ds, ax
        mov cx, 3
        sub si, si
        lea bx, Array
        mov bx, offset Array
        jcxz Ended
        mov cx, 3
For1:   mov dx, [bx +si]
        mov Count, cx
        add si, 2
        _Putstr tested  
        
While1: mov ax, 0
        cmp ax, dx 
        je EndWhile1
        mov Val, dx
        _PutCh '#'
        mov dx, Val
        dec dx 
        ;inc ax
        jmp While1

EndWhile1:  
        ;add si, 2
        mov cx, Count
        dec cx
        jnz For1
        
Ended:   _exit 0

PrintChar endp

end PrintChar

    
    
  • For starters, `Array` should be `DW` not `DB`. – Jester Nov 24 '20 at 21:20
  • 1
    what have you done to debug this and show where you divided and tested the code segments. what your algorithm was before you coded it, etc (ideally prototyped in a high level language and tested then ported to assembly language). – old_timer Nov 24 '20 at 21:23
  • 2
    If you think the error is with the printing, you should make a minimal example that only prints a constant amount of characters and see if that works. You don't need the input and maximum and stuff to test printing. – Jester Nov 24 '20 at 21:24
  • @old_timer I didn't include the segments, but the program was working as it should before I added the PrintChar procedure. That was the only thing to change. I didn't code in a high-level language yet as my converter does not convert to the right assembly language :( – youngtirith Nov 24 '20 at 21:27
  • The high level language is how one would prototype their algorithm. Two problems with an assignment like this. The algorithm, and then the implementation in the target language. If you divide the problem in those two parts rather than try to do it all at once you have a better chance at success with the least effort. You have provided nothing for us to work with, we are not here to debug homework assignments, you have to do most of the work... – old_timer Nov 24 '20 at 21:29
  • Also note that `Display` destroys `Val` for no obvious reason, so by the time you get to `PrintChar` it's no longer the maximum. – Jester Nov 24 '20 at 21:30
  • sounds like you need to make a new program that only tests the printchar routine without any of this other code then merge that in once debugged. which is another thing one would do with an assignment like this, build the parts test as you go so that every few new lines is well tested before adding any more. Or develop blocks/functions separately, test them then glue them together. – old_timer Nov 24 '20 at 21:30
  • If it was working before you added a new block then test the new block and then verify the interface to the new block (the call, the passed parameters, shared registers that might get clobbered on, etc). – old_timer Nov 24 '20 at 21:32
  • The `For2` label is in the wrong place, and the second half of `PrintChar` that's supposed to print half as many `#` is simply nonsense. As for the endless loop, make sure `_PutCh` does not destroy `cx` or `dx`. – Jester Nov 24 '20 at 21:37
  • @Jester I have brokenn it up and am trying to see if there is an easier way to code it without the loops? I appreciate your help! I am new to assembly and don't know if it is possible to use the dup function to print instead of the loop? like _PutCh ax dup('#') – youngtirith Nov 24 '20 at 21:41
  • No, you need a loop as the number is only determined at runtime. – Jester Nov 24 '20 at 21:44
  • @jester I am still getting stuck... it either gets stuck in the loop or only goes through it once, is there anywhere you are seeing that it looks wrong? I cannot figure out where I am going wrong and I have been trying multi different tactics the last two hours – youngtirith Nov 24 '20 at 23:50
  • 1
    In your current version `For1` reloads `cx` with 3, so it will indeed be an endless loop. That label should probably be on the `mov dx, [bx+si]` line. – Jester Nov 25 '20 at 00:01
  • @Jester Thank you!! I just have to fix the while loop now. I couldn't get past the endless loop, I really appreciate it! – youngtirith Nov 25 '20 at 00:13
  • 1
    So the code in the question isn't buggy anymore, and doesn't match the text? Now there's no real question here. Don't edit questions to make them non-questions. Either delete it or post an answer as an answer, not an edit to the question. – Peter Cordes Nov 25 '20 at 04:07

1 Answers1

0
include pcmac.inc
        .MODEL  SMALL
        .586
        .STACK  100h
        .DATA
        
Val     DW 6
Count   DW 3
Array     DW 1, 2, 80
DisplayCH DB 13, 10, 'Each # respresents ' , '$'
Tested    DB 13, 10, 'Test' , '$'
        
        .CODE
        extrn GetDec:near
        extrn PutDec:near

PrintChar Proc
        _begin
        mov ax, @data
        mov ds, ax
        mov cx, 3
        sub si, si
        lea bx, Array
        mov bx, offset Array
        _PutStr DisplayCH
        jcxz Ended
        mov cx, 3
For1:   _PutCh 13,10
        mov dx, [bx +si]
        mov Count, cx
        
While1: mov ax, 0
        cmp ax, dx 
        je EndWhile1
        mov Val, dx
        _PutCh '#'
        mov dx, Val
        dec dx 
        jmp While1

EndWhile1:  
        add si, 2
        mov cx, Count
        dec cx
        jnz For1
        
Ended:   _exit 0

PrintChar endp

end PrintChar
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 1
    It's nice to see that you have a working solution. However your answer should **explain in your own words** how this version solved the original problem and not only show the final code (and without embedded comments no less...) – Sep Roland Nov 26 '20 at 17:43