;===================================================== ; LMC Sample Program #6 - negative flag and a FOR loop ;===================================================== ;- Ian! D. Allen - idallen@idallen.ca - www.idallen.com ; ; Some deceptively simple code, translated to LMC assembly language. ; ; int cnt; ; for ( cnt = 10; cnt > 0; cnt -= 2 ) { ; cout << cnt; ; } ; ; This FOR loop must be rewritten as a WHILE loop first: ; ; int cnt; ; cnt = 10; ; while ( cnt > 0 ) { ; cout << cnt; ; cnt -= 2; ; } ; ; Warning! ; The LMC computer has no negative numbers, only a Negative flag! ; In the LMC, "X >= 0" is always true, and "X < 0" is always false. ; ; In the LMC, testing "X <= 0" is the same as testing "X == 0". ; In the LMC, testing "X > 0" is the same as testing "X != 0". ; These limitations affect loop logic. If your loop never actually ; sets X to exactly zero, the loop never terminates. ; ; This loop (very similar to the above loop) never stops (why?): ; ; // Infinite Loop under LMC or under unsigned math on a real computer ; for ( cnt = 11; cnt > 0; cnt -= 2 ) { ; cout << cnt; ; } ; ;Label Mnem. Operand Comments............. ;----- ----- ------- --------------------- LDA TEN ; initialization section of FOR loop (done ONCE) STO CNT TEST LDA ZERO ; test section of FOR loop (repeated) SUB CNT SKN ; skip *into* loop if cnt > 0 (same as if cnt != 0) JMP ENDFOR ; exit FOR loop if cnt <= 0 (same as if cnt == 0) LDA CNT ; body of FOR loop (repeated) OUT LDA CNT ; decrement section of FOR loop (repeated) SUB TWO ; this is where the Negative flag might come on STO CNT JMP TEST ; jump to loop test (not to initialization) to repeat ENDFOR HLT ; constants: ZERO DAT 000 TWO DAT 002 TEN DAT 010 ; variables: CNT DAT ?