Struc - Defining Data Structures

C++ provides user defined data types and structures. The struc statement defines the data structure fields and the '.' operator provides access to the structure fields. Assembler provides very similar structure definitions and access operations.

In the following, a person structure is defined, 5 person variables are created and initialized. The firstname and age field of one of the structures is printed. It is instructive to examine the memory layout of a structure. The P1 person structure is in memory below, P1.age is offset from the start of P1 by 6 bytes (1316=1910) and P1.next is offset by 10 (or A16) bytes (0000003816 is the address of P2).

P1
firstname             age             next
M a r y     13 00 00 00 38 00 00 00
 0  1  2  3   4  5   6  7   8  9   A  B  C  D

The following prints the firstname and age of the P1 structure, Mary and 19.

#include <iostream.h>

struct person {
	char   *firstname;
	int    age;
	person *next;
};


 person P5 = {"Harry ",22,NULL};
 person P4 = {"Sally ",24,&P5};
 person P3 = {"Elmer ",20,&P4};	// Mary->John->Elmer->
 person P2 = {"John  ",18,&P3};
 person P1 = {"Mary  ",19,&P2};	// Sally->Harry->NULL

void main(void) {
   cout << P1.firstname << "\n";
   cout << P1.age;
}
NULL    equ      -1          

person 	struc                        
       	firstname db       6 dup (?)
       	age       dword    ?
       	next      dword    NULL	
person 	ends	

.data   
  P5  person      {"Harry ",22,NULL}
  P4  person      {"Sally ",24,P5}
  P3  person      {"Elmer ",20,P4} 
  P2  person      {"John  ",18,P3}
  P1  person      {"Mary  ",19,P2}  

.code
main 	Proc	near 	
	Lea	eDi, P1.firstname
	Mov	eCx, 6			
	Call	PutStrng	
	Call	NewLine	
	Mov	eAx, P1.age
	Call	PutDec

	invoke	ExitProcess, 0
main	Endp
        End     main

Linked Lists - The next field of each person is a reference to another person constructing a linked list from P1->P2->P3->P4->P5. The list person first name field is printed by iterating through each person until a NULL is reached.

#include <iostream.h>

struct person {
	char   *firstname;
	int    age;
	person *next;
};


 person P5 = {"Harry ",22,NULL};
 person P4 = {"Sally ",24,&P5};
 person P3 = {"Elmer ",20,&P4};	// Mary->John->Elmer->
 person P2 = {"John  ",18,&P3};
 person P1 = {"Mary  ",19,&P2};	// Sally->Harry->NULL

void main(void) {

 person *eBx = &P1;
	
 do{
   cout << eBx->firstname << "\n";
   eBx = eBx->next;
 } while (eBx != NULL);
}
NULL    equ      -1          

person 	struc                        
       	firstname db       6 dup (?)
       	age       dword    ?
       	next      dword    NULL	
person 	ends	

.data   
  P5  person      {"Harry ",22,NULL}
  P4  person      {"Sally ",24,P5}
  P3  person      {"Elmer ",20,P4} 
  P2  person      {"John  ",18,P3}
  P1  person      {"Mary  ",19,P2}  

.code
main 	Proc	near 
	Mov	eBx, offset P1		
  @@do: 				
	Lea	eDi, [eBx].person.firstname
	Mov	eCx, 6			
	Call	PutStrng	
	Call	NewLine	
	Mov	eBx, [eBx].person.next	
  @@while:				
	Cmp	eBx, NULL
	Jne	@@do
  @@endwhile:

	invoke	ExitProcess, 0
main	Endp
       End     main

Recursion on Data Structures

The same linked list structure is used from above but recursion replaces iteration as the means of traversing the list. Note that in C++ and Assembler, the address of a person variable is passed to the Print function, that is call-by-reference.

#include <iostream.h>

struct person {
	char   *firstname;
	int    age;
	person *next;
};


  person P5 = {"Harry ",22,NULL};
  person P4 = {"Sally ",24,&P5};
  person P3 = {"Elmer ",20,&P4}; // Mary->John->Elmer->
  person P2 = {"John  ",18,&P3};
  person P1 = {"Mary  ",19,&P2}; // Sally->Harry->NULL


void Print(person *P) {
	person *eBx=P;

	if(eBx != NULL) {
		cout << eBx->firstname << "\n";
		Print(eBx->next);
	}
}









void main(void) {

  Print(&P1);
}
NULL    equ      -1          

person 	struc                        
       	firstname db       6 dup (?)
       	age       dword    ?
       	next      dword    NULL	
person 	ends	

.data   
  P5  person      {"Harry ",22,NULL}
  P4  person      {"Sally ",24,P5}
  P3  person      {"Elmer ",20,P4} 
  P2  person      {"John  ",18,P3}
  P1  person      {"Mary  ",19,P2}  

.code
Print   Proc    near, P : near ptr person 
        Mov     eBx, P

 @@if:  Cmp    	eBx, NULL       
        Jne     @@then     
        Jmp     @@endif    
  @@then:
        Lea    	eDi, [eBx].person.firstname
        Mov     eCx, 6
        Call    Putstrng
        Call   	NewLine

        invoke	Print, [eBx].person.next
 @@endIf:
	Ret
Print   Endp

main 	Proc	near 
	invoke	Print, addr P1

	invoke	ExitProcess, 0
main	Endp
       End     main

Document last modified: