I'm dealing with the assembler task from school. We got 3 programs and this is one of them. i'm trying to answear questions, but it would be way easier by knowning anything about assembler before (no matter how nonsense it sounds, our teacher really didn't teach us anything).
Given that the 2-byte number contains of 3 (lsb) and 4(msb):
It would be such easier to understand how the multiplication is working really, because I have to project the final value as 24-bit number.
1 ;main.s03
2
3 name MAIN
4 extern _R,MUL21
5 rseg CODE
6 mov PSW,#_R
7 mov SP,#BSTACK-1
8
9 mov R0,#3 ;number 1, lsb
10 mov R1,#4 ; msb
11 mov R2,#16 ;number 2
12 lcall MUL21 ;result R6,R5,R4
13
14 sjmp $
15
16 rseg ISTACK
17 BSTACK: ds 8
18
19 end
1 ;mul21.s03
2
3 name MUL21
4 extern _R
5 public MUL21
6 rseg CODE
7 MUL21: mov A,R0
8 mov B,R2
9 mul AB
10 mov R4,A
11 mov R5,B ;R5,R4=R0*R2
12
13 mov A,R1
14 mov B,R2
15 mul AB
16 add A,R5
17 mov R5,A
18 mov A,B
19 addc A,#0
20 mov R6,A ;R6,R5+=R1*R2,R4
21
22 ret
23
24 end
Follow each instruction to see what it's effect on the program state is (A & B registers & global variables Rn).
Most of the instructions are simple, just copying an 8-bit value from one place to another. Keep a scratchpad showing the latest values at each location/step in the machine code program, and update that scratchpad for each instruction.
The 8051 mul AB
instruction does 8-bit × 8-bit yielding a 16-bit result with low 8 bits of that in A, and high 8 bits in B.
Don't worry about the algorithm for now (and terminology MSB, LSB), just concentrate on the 8-bit values and how they move / are generated by each instruction.
Kind of separately, we can look at the algorithm, and the input values and output value. Hex is a good to use when larger values (16-bit and 24-bit) are split across 8-bit bytes (as two 4-bit hex digits is one complete byte — to do with decimal, need more math). The input is LSB 0x03 and MSB 0x04, so the 16-bit value is 0x0403, which is 1027 (decimal). The algorithm multiples both halves (LSB, MSB) by the 8-bit multiplier (here 16), and then adds them together along with a carry that should be propagated. As you worked out, the final output is 3 bytes, in hex 0x00, 0x40, 0x30, or as 24-bit number, 0x004030, which is 16342 (decimal). As you can see, 0x0403 multiplied by 16 (which is "10" in hex) is 0x4030.