UMBC CMSC 211

UMBC | CSEE


Pointers

Pointers in high-level languages (like, C, C++, Java) are just memory addresses. But the size of segments and the locations are unknown, so they are in the segment:offset form. That take 32 bits, or a double word, and are known as far pointers. (Single word pointers are known as near pointers and can only be used within one segment.)

To do this, we must declare them as:

X DW? ; an integer variable
PX DDX ; a far pointer initialized to
; the address of X
PP DD? ; an uninitialized far pointer
LongIntDD123456; a 32-bit integer (long int)

 

The null pointer (NULL in C) is supposed to point to nothing and is used to signify a special condition, must be a value that is not allowed to occur, 0:0.

There two special instructions to put a far pointer into a register pair:

ldsreg, DWordVbl ; sets ds:reg to the value of DWordVbl
lesreg, DWordVbl ; sets es:reg to the value of DWordVbl

 

This assumes that the offset part of the addres comes first in memory, which DD will do for you, and is consistent with the little-endian ordering of bytes. There is no special reverse instruction, to put the register pair into a memory variable. That must be done like this:

movWORD PTR PP, bx ; set PP to es:bx
movWORD PTR PP + 2, es ;

Traverse a linked list.

Suppose that the double word pointer First points to the first element of a linked list. Each node points an element with the name NEXT that points to the next node. We can process the list with the following code:

  les bx, First ; es:bx points to
; the first node
  ASSSUME es : NOTHING ; At assembly time we don't know
; es's segment
ListLoop:     ;
  test bx, bx ; Is es:bx null?
  jnz GoOn ;
  mov ax, es ;
  test ax, ax ; can't test a segment register
  jz Done ;
GoOn:     ;
    ; process node here ;
  les bx, es:[ NEXT + bx ] ; Get the next pointer
Done:     ;


UMBC | CSEE |