Amitay Tsinis
Amitay Tsinis

Reputation: 330

How can I determine the result in AX after MOV and LEA instructions

I am trying to understand what will be the content of AX register in the following question, I don't understand how I can know what [5000h] or [DI] is in the examples.

The state of the registers and memory are defined as:

CS=3000 [53000]=BBBB [33000]=6666 [13000]=1111
DS=1000 [54000]=CCCC [34000]=7777 [14000]=2222
SS=5000 [55000]=DDDD [35000]=8888 [15000]=3333
DI=7000 [56000]=EEEE [36000]=9999 [16000]=4444
BP=4000 [57000]=FFFF [37000]=AAAA [17000]=5555

What is the value in AX for each of these instructions

Upvotes: 4

Views: 865

Answers (2)

Michael Petch
Michael Petch

Reputation: 47573

This is an academic question, but it touches on a number of concepts of real mode 20-bit segment:offset addressing. All memory addresses in real mode are always made up of two parts - a segment and an offset. The two parts are combined together to generate a physical address with the formula:

Physical Address = segment * 16 + offset

or

Physical Address = segment << 4 + offset

Both yield the same result as shifting something left 4 bits is the same as multiplying by 16 decimal (or 10h hexadecimal).

You will find that instructions may specify a segment explicitly and when it isn't specified there is always an implicit one. A general rule is that if a memory address uses BP then the memory operand is relative to the SS segment, otherwise it is relative to the DS segment.

An LEA instruction doesn't actually access physical memory, it simply computes the effective address of the memory operand and loads the address in a register. With LEA the segment doesn't come into play. A MOV instruction with a memory operand will move the contents of a memory operand to/from a register.


All the values given in your questions are given in hexadecimal. To answer your questions:

  • MOV AX, [DI] is the same as MOV AX, [DS:DI] since the implied segment is DS. In the question DS=1000h. DI=7000h . The offset is DI. Using the formula segment<<4 + offset we get physical address 1000h<<4+7000h = 10000h+7000h=17000h. The question states memory address [17000]=5555 so the value moved to AX is 5555h.

  • MOV AX, [5000h] is the same as MOV AX, [DS:5000h] since the implied segment is DS. In the question DS=1000h. The offset is 5000h . Using the formula segment<<4 + offset we get physical address 1000h<<4+5000h = 10000h+5000h=15000h. The question states memory address [15000]=3333 so the value moved to AX is 3333h.

  • MOV AX, [BP+2000h] is the same as MOV AX, [SS:BP+2000h] since the implied segment is SS. In the question SS=5000h and BP=4000h. The offset is BP+2000h . Using the formula segment<<4 + offset we get physical address 5000h<<4+(4000h+2000h) = 50000h+(4000h+2000h)=56000h. The question states memory address [56000]=EEEE so the value moved to AX is EEEEh.

  • LEA AX, [BP+1000h] : The segment doesn't come into play since it is an LEA instruction. In the question BP=4000h. The offset is BP+1000h=4000h+1000h = 5000h. Since LEA only computes and stores the address in a register the value in AX will be 5000h.

Upvotes: 7

thb
thb

Reputation: 14454

[My answer is left here for reference but I withdraw it. From the information you have given, I gather that your x86 processor is operating in privileged 8086 compatibility mode, as during boot loading. I have no experience at writing bootloaders, unfortunately.]

Old data in a register is overwritten when new data arrives. Therefore, only the LEA instruction affects this result.

Moreover, the LEA instruction is special: it does not dereference the address it computes. In your example, because BP contains 4000h, the address the LEA computes is 4000h + 1000h == 5000h. The last address is not used, but is merely stored for future use in the AX register.

Therefore, at the end of this code's execution, the register AX will hold the value 5000h.

To clarify, I did not say that the register AX will hold a copy of the datum stored in memory at address 5000h. Rather, I said something simpler: the register AX will hold the value 5000h.

Upvotes: 2

Related Questions