0
MVI B 07h
LXI H 0007h
LXI D 0007h
DCR B
LOOP1: DCR B
MOV C B
INR B
LOOP: DAD D
DCR C
JNZ LOOP
MOV E L
MOV D H
DCR B
JNZ LOOP1
HLT

I couldn't find out the problem in my code. Can you please help me out? It's giving me partially wrong answer. The two LSB bits are correct but not the MSBs.

Sam
  • 3,320
  • 1
  • 17
  • 22
Soumya Kanti Naskar
  • 1,021
  • 3
  • 16
  • 29
  • 1
    Can you add little bit more info? For example what are you trying to do with these code, what is the expected answer and what went wrong from expected one? – Ramsharan Dec 29 '15 at 03:37
  • Why not try writing it in plain C first, and then translate it to assembly? – Sam Dec 29 '15 at 04:07

1 Answers1

1

I'm not sure why you're doing the extra decrement (followed by the increment) at the LOOP1 label to the B register but when B is one it causes C to become 0, which then wraps around to FFh and performs the multiply loop another 255 times.

Instead why don't you take out the DCR B / INR B and before the multiply loop just set the H register to 0. The full program would look like this:

MVI B, 07h
LXI H, 0007h
LXI D, 0007h
DCR B

LOOP1:
    MOV C, B
    LXI H, 0

    LOOP:
        DAD D
        DCR C
        JNZ LOOP

    MOV E, L
    MOV D, H
    DCR B
    JNZ LOOP1

HLT
Sam
  • 3,320
  • 1
  • 17
  • 22
  • It helps me a lot. Actually my approach for the loop is something different. As (H)(L) & (D)(E) register pair both contains the same value , for the first case let's say 7, 6 no of '7' s must be added. So we can run a loop for 5 times with the command 'DAD D', which will serve the purpose. But your approach is simpler, and also for mine it encounters a problem you have mentioned earlier. – Soumya Kanti Naskar Dec 29 '15 at 15:27