We recently looked at how to do multiplication using the Little Man Computer instructions. It will help with what we are about to do if you have read and understood that article. A key concept from that article was the need to be very clear about the different roles played by the multiplier and the multiplicand. A similar distinction is needed with division. We have the dividend, which is the number to be divided, and the divisor, which is the number we are dividing by. In our program, we will repeatedly subtract the divisor, keeping track of how many times we do so, until we reach zero. For now we will only use numbers where the divisor is a factor of the dividend (i.e. it “goes into” the dividend exactly). Here is our pseudocode:
1 2 3 4 5 6 7 8 |
READ DIVIDEND, DIVISOR QUOTIENT = 0 WHILE DIVIDEND > 0: DIVIDEND = DIVIDEND - DIVISOR QUOTIENT = QUOTIENT + 1 ENDWHILE PRINT QUOTIENT |
The Little Man Computer instructions are below, along with comments. Try to understand exactly what each line does and how. If you get stuck, let me know in the comments and I’ll try and help. There is a great LMC simulator available here: Little Man Computer for you to use. Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Get user input INP DIVIDEND STA DIVIDEND INP DIVISOR STA DIVISOR // Begin loop LOOP LDA DIVIDEND // If we have reached 0, jump to END BRZ END // Subtract divisor from dividend and store the result SUB DIVISOR STA DIVIDEND // Increase the count (QUOTIENT) and store LDA QUOTIENT ADD ONE STA QUOTIENT // Jump to start of loop BRA LOOP // We've finished, so display the result END LDA QUOTIENT OUT SUB QUOTIENT STA QUOTIENT HLT // Variables DIVIDEND DAT 0 DIVISOR DAT 0 QUOTIENT DAT 0 ONE DAT 1 |
Little Man Computer Programming Teaching Pack for Computer Science GCSE and A Level
You can check out a great resource for teaching or learning about Little Man Computer Programming by clicking here or on the image below.