Search code examples
assemblymasmx86-16tasm

Direct and Indirect addressing and OFFSET


I am fairly new to assembly and I am trying to work out this question from a past exam. I am stuck in need of help, apologies if this is basic.

I have the following code:

                 .MODEL medium
                 .STACK
0000             .DATA
0000 04D2 10E1   Count dw 1234,4321
0000            .CODE
                .STARTUP
0017 BB 0000 R   mov bx, OFFSET Count
001A B8 000A     mov ax,10
001D 8B C3       mov ax,bx
001F 8B 07       mov ax,[bx]
0021 A1 0000 R   mov ax,Count
.EXIT
END

I am asked to identify the different types of addressing, which I can do and then state the value of ax after each instruction.

In particular I do not know how to work out the last 3 instructions. So my questions are:

  1. What value is stored in bx? Is it the address of Count? How do I find this address?

  2. After the instruction mov ax,[bx] I am fairly certain ax contains the value of Count. What is this value?

  3. mov ax,Count Is this the same as 2?


Solution

  • You've been too busy. Your teacher will explain that in detail. The buzzwords are "segment/offset" and "organization of an .exe program in memory". Briefly:

    1. bx gets the offset of Count.

      A x86-16 address is split into two parts: segment and offset. My Turbo Debugger sets Count to the address 1603:0000. The first (hexadecimal) number indicates the segment, the second the offset. The segment will be calculated by the operating system when it loads the program and can change with every program run. The .STARTUP directive produces code which assigns the calculated value to the segment register DS (don't confuse it with the general purpose register DX).

      The offset is the relative distance from the start of that segment address. Since Count is at the beginning of the .DATA segment its relative distance from the start of the segment is 0000. So, BX=0000.

    2. The values of Count are initialized by Count dw 1234,4321. Count is not a variable, but a label. At this label you can find two words: 1234 and 4321. mov ax,[bx] will load a word from the address DS:0000 and this is the address of Count.

      Your assumption is right, mov ax,[bx] contains the first value of Count = 1234. The number is decimal.

    3. This is assembler specific. In MASM syntax it is the same as 2, in NASM syntax it is the same as 1. The code is obviously MASM, so it is the same as 2. It is in the end the same: The instructions are different.