8085 assembly #3: Task #2
8/1/2018
For excercise 2 and a few in the future I wanted to focus on implementing basic math functions. This task focuses on multiplication. Multiplication is nothing more than repeated addition, so, when broken down it's really quite simple. This implementation focuses on a two number operation, you need a multiplicand and a multiplier. In my program N1 stores the multiplican, N2 the multiplier. The steps to achieve N1*N2 are simple, add N1 to itself N2 times. I do not determine which number is bigger and reduce the number of iterations, although this may be a task for another day.
I produced two versions of todays task. The first is longer and over complicated, the second more simple and shorter. After I completed version #1 I realised a few things I could change and came up with version #2.
In particular I realised using a branching instructor is not the only way to determine if a number is zero, the status flags are in fact set when I do my DCR (decrement) operation. So the second interation does that, which saves a few lines of instructions (storing A, putting 00H in its place, comparing it to C and loading the original A back).
Iteration #1
; MULTIPLY TWO NUMBERS TOGETHER, STORE IN MEMORY AT 00H ; MULTIPLICATION IS NOTHING MORE THAN REPEATED ADDITION JMP START N1: EQU 05H ; MULTIPLICAND N2: EQU 03H ; MULTIPLIER N3: EQU 00H ; MEMORY LOCATION TO STORE RESULT START: LXI H,N3 ; H AS MEMORY POINTER TO OUTPUT LOCATION 00H MVI C,0 ; CLEAR C MOV B,C ; CLEAR B MOV A,B ; CLEAR A MVI C,N2 ; MOVE N2 INTO C, THIS IS OUR COUNTER ADD: MOV B,A ; STORE A IN B MVI A,00H ; STORE 00H IN A CMP C ; COMPARE C TO A MOV A,B ; PUT B BACK INTO A JZ END ; IF C == 0 JUMP TO END ADI N1 ; ADD N1 TO A DCR C ; DECREMENT C JMP ADD ; JUMP TO ADD END: STA N3 ; STORE A HLT ; HALTIteration #2 (the better way)
; MULTIPLY TWO NUMBERS TOGETHER, STORE IN MEMORY AT 00H ; MULTIPLICATION IS NOTHING MORE THAN REPEATED ADDITION JMP START N1: EQU 05H ; MULTIPLICAND N2: EQU 03H ; MULTIPLIER N3: EQU 00H ; MEMORY LOCATION TO STORE RESULT START: LXI H,N3 ; H AS MEMORY POINTER TO OUTPUT LOCATION 00H MVI C,0 ; CLEAR C MOV A,C ; CLEAR A MVI C,N2 ; MOVE N2 INTO C, THIS IS OUR COUNTER ADD: ADI N1 ; ADD N1 TO A DCR C ; DECREMENT C JNZ ADD ; IF C == 0 JUMP TO END STA N3 ; STORE A IN MEMORY AT N3 HLT ; HALT