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
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.