Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • MOV AX, [DI]
  • MOV AX, [5000h]
  • MOV AX, [BP+2000h]
  • LEA AX, [BP+1000h]
like image 657
Amitay Tsinis Avatar asked Jan 01 '23 08:01

Amitay Tsinis


1 Answers

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.

like image 96
Michael Petch Avatar answered Jan 13 '23 13:01

Michael Petch