Electronic – How to use structures in 8086 (MASM)

assemblymicroprocessorprogramming

Sample program to add two numbers using structures –

DATA SEGMENT
  ORG 1000H
  FOO STRUC
    A DB 0FFH
    B DB 0FFH        
    SUM DW ?
  FOO ENDS
DATA ENDS

CODE SEGMENT
  ASSUME CS:CODE,DS:DATA
  START:MOV AX,DATA
        MOV DS,AX

        XOR AX,AX
        MOV AL,FOO.A   
        ADD AL,FOO.B
        ADC AH,00H
        MOV DS:FOO.SUM,AX
        HLT

CODE ENDS
END START

No errors and warnings
but while debugging AL is assigned with 00H

Best Answer

You're using the wrong addressing mode. Your instruction

MOV AL,FOO.A

will load the (lower byte) of the address of FOO.A in AL. This is called "immediate addressing mode".

Change this into

MOV AL,[FOO.A]

to use the "direct addressing mode" which will take the expression FOO.A as an address to the value to be loaded.


While debugging you should have watched that

ADD AL,FOO.B

adds the value "1".