1

I want to store the address of the largest number in HL but I don't really know how to do it This is what i've done so far

   0000 LXI H,3000H       ;Load H-L pair with address 3000H
   0001
   0002
   0003 MOV E,M      ;Move counter from memory to reg. E.
   0004 INX H        ;Increment H-L pair
   0005 MOV A,M      ;Move the 1st number from memory to reg. A.
   0006 DCR E        ;Decrement counter.
   0007 INX H        ;Increment H-L pair
   0008 MOV D,M      ;Move the next number from memory to reg. D.
   0009 CMP D        ;Compare D with A.
   000A JNC 000EH    ;Jump to address 000EH if there is no carry
   000B
   000C
   000D MOV A,D      ;Move largest from reg. D to reg. A.
   000E DCR E        ;Decrement counter.
   000F JNZ 0007H    ;Jump to address 0007H if counter is not zero.
   0010
   0011
   0012 INX H        ;Increment H-L pair.
   0013 MOV C,A      ;Move the result from reg. A to C
   0014 HLT


**MEMORY**  
3000H: 05 (counter)
3001H: 2C
3002H: 1E
3003H: 58
3004H: 46
3005H: 53

The code works fine in the part of finding the maximum number but what I want is to also store the address of the maximum number in HL in the end

  • It's good that you commented your code, but what exactly doesn't work if you run this? (See [mcve]). Also, can you fix the formatting? (remove double-spacing, then select all your code and click the `{}` icon to make it a code block). – Peter Cordes Dec 23 '17 at 20:51
  • The code works but, I would like to store the address with the maximum value. In this case, after running the program I want HL to store the address 3003H because it's the one with the highest value. –  Dec 23 '17 at 21:36
  • Nice edit, that's a huge improvement in readability and the paragraph at the end makes this into a real question. – Peter Cordes Dec 23 '17 at 21:36
  • 1
    Looks to me like `BC` is spare during loop (if 8085 has similar registers to Z80), so you can copy `HL` to it whenever you find new maximum, then at end copy `BC` to `HL`. – Ped7g Dec 23 '17 at 22:26

1 Answers1

1

I don't know 8085 very well so I'll just give a general answer that works for any register machine. (Or in C or whatever: this is one of those questions where the answer is "the same as you would in any other language")

When you find a new maximum, copy the address somewhere as well as the value. (In the same block of instructions with MOV A,D, which you conditionally jump over). You still need the value for comparisons.

If 8085 doesn't have enough registers, store it to memory. When your loop is done, you can reload HL from there if / when you want. Of leave the value in memory as the result of your function.

Make sure you init both the starting value and address to the first element, in case it's the max. Unlike a value-only search, you can't just use the minimum possible value as an initializer for the max.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847