2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Gotta find out some alumnus from 80s to find the bug in this ... \$\endgroup\$ – Mitu Raj Nov 13 '19 at 10:40
5
\$\begingroup\$

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".

\$\endgroup\$
  • \$\begingroup\$ ya,It works with "direct addressing mode" like ``` MOV AL,DS:[FOO.A]``` and point to be noted structure can't be initialized like A DB 0FFH it result in garbage value. It should be declared as A DB ? \$\endgroup\$ – Sathvik Nov 13 '19 at 12:56
  • \$\begingroup\$ Good find ......... \$\endgroup\$ – Mitu Raj Nov 13 '19 at 14:44
1
\$\begingroup\$

Just answering my question to tell few important things while using struct

  1. It can't be initialized (If initialized results in garbage values)

  2. It should be accessed using "direct addressing mode" (If not result in "immediate addressing mode")

DATA SEGMENT
FOO STRUC
A DB ?
B DB ?       
SUM DW ?
FOO ENDS

DATA ENDS

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

      XOR AX,AX
      MOV DS:[FOO.A],0FFH
      MOV DS:[FOO.B],0FFH

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

CODE ENDS
END START
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.