i want to add two values in memory. I have to store the letters for A-J in memory location from 100 to 110 and i have to store two letters in single step, for example AH=A Al=B
and to increase Bx +2. Help 🙂 Here is my code by i don’t think that is working well. I work in Emu 8086.
MOV AX, data
MOV DS, AX
MOV Ah,40h
Mov Al,41h
MOV BX, 0100h
AGAIN:
MOV [BX], Ax
inc Ah
Inc Al
add Bx,2
cmp Ah, Al
JNE AGAIN
Thank you.
80×86 is “little-endian” which means that the value in AL would be stored at the lowest address and the value in AH would be stored in the highest address. This means your code would do “BADCFEHGJI”.
If AL and AH start out being different; and you increment them both inside the loop, then they will never have the same value at the end of the loop and your program will run forever (or until it crashes, whichever comes first).
Rather than incrementing AL and AH separately; you could do
add ax,0x0101. This works (and will probably be twice as fast as the pair of increments) because you’re not expecting either of them to overflow. The same idea applies tomov al,40handmov ah,41h, which could be a singlemov ax,4140h(and probably should bemov ax,('B'<< 8) | 'A'to make it easier to understand).Finally, there’s a very old optimisation called “loop unrolling”. The idea is to reduce the overhead of the loop by doing more inside the loop. In your case, because the loop is so small (and everything is constant) it’d be easy to “fully unroll” and not have any loop at all. For example: