;===================================================== ; 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. ; ; for ( cnt = 10; cnt > 0; cnt -= 2 ) { ; cout << cnt; ; } ; ; This FOR loop must be rewritten as a WHILE loop first: ; ; cnt = 10; ; while ( cnt > 0 ) { ; cout << cnt; ; cnt -= 2; ; } ; ; The WHILE loop must be rewritten using LMC "skip" and "jump": ; ; cnt = 10 ; WHILE: calculate zero - cnt (to set LMC lights) ; if ( negative light is on ) skip next instruction ; jump to ENDWH (jump over WHILE body) ; output cnt ; cnt = cnt - 2 ; jump to WHILE ; ENDWH: halt ; ; 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............. ;----- ----- ------- --------------------- ; for ( cnt = 10; ; initialization section of FOR loop (done ONCE) LDA TEN ; cnt = 10 STO CNT ; cnt > 0; ; test section of FOR loop (repeated) WHILE LDA ZERO ; while( cnt > 0 ) SUB CNT SKN ; skip *into* loop if cnt > 0 (same as if cnt != 0) JMP ENDWH ; exit loop if cnt <= 0 (same as if cnt == 0) ; cout << cnt; ; body of FOR loop (repeated) LDA CNT ; cout << cnt OUT ; cnt -= 2 ; decrement section of FOR loop (repeated) LDA CNT ; cnt -= 2 SUB TWO STO CNT JMP WHILE ; jump to loop test (not to initialization) to repeat ENDWH HLT ZERO DAT 000 ; constant TWO DAT 002 ; constant TEN DAT 010 ; constant CNT DAT ; variable ------------------------------------------------------------------------------- Assembly Language "Listing" file showing mailboxes (MB) and Machine Code: MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 115 : LDA TEN ; cnt = 10 01 216 : STO CNT 02 113 : WHILE LDA ZERO ; while( cnt > 0 ) 03 416 : SUB CNT 04 800 : SKN ; skip *into* loop if cnt > 0 05 912 : JMP ENDWH ; exit loop if cnt <= 0 06 116 : LDA CNT ; cout << cnt 07 600 : OUT 08 116 : LDA CNT ; cnt -= 2 09 414 : SUB TWO 10 216 : STO CNT 11 902 : JMP WHILE ; jump to loop test (not to initialization) 12 700 : ENDWH HLT 13 000 : ZERO DAT 000 ; constant 14 002 : TWO DAT 002 ; constant 15 010 : TEN DAT 010 ; constant 16 000 : CNT DAT ; variable Label Table (created during first pass through source code): Label Memory Location ------- --------------- WHILE at 02 ENDWH at 12 ZERO at 13 TWO at 14 TEN at 15 CNT at 16 -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/