Vahid Kharazi
Vahid Kharazi

Reputation: 6033

struct or class in assembly

I need something like struct or class in c++

For example I need a class with an array and two attribute (size and len) and some function like append and remove .

How can I implement this in assembly with macros and procedures?

Upvotes: 4

Views: 10244

Answers (3)

Sathvik
Sathvik

Reputation: 654

Structure in 8086 MASM

syntax

struct_name STRUC
var_name type ?
...
struct_name ENDS

Rules

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

program to add two numbers

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]   ;al=ff
      ADD AL,DS:[FOO.B]   ;al=al+ff
      ADC AH,00H          ;ah=ah+carry_flag(1/0)+00
      MOV DS:[FOO.SUM],AX ;sum=ax
      HLT                 ;stop

CODE ENDS
END START

Upvotes: 2

Aki Suihkonen
Aki Suihkonen

Reputation: 20087

Tasm supports eg.

struc String  // note: without 't' at the end
   size   dw 100
   len    dw 10
   data   db 0 dup(100)
ends String

Gnu assembler also has a .struct directive.

The syntax for MASM is:

String STRUCT
    size dw 100
    len dw 10
String ENDS

Usage again from the same MASM manual:

ASSUME eax:PTR String
mov ecx, [eax].size,
mov edx, [eax].len
ASSUME eax:nothing
.. or ..
 mov ecx, (String PTR [eax]).size   // One can 'cast' to struct pointer

One can also access a local variable directly

mov eax, myStruct.len

Upvotes: 7

Brian Knoblauch
Brian Knoblauch

Reputation: 21409

Here's a sample MASM struct from a HID interface routine that I wrote:

SP_DEVICE_INTERFACE_DATA struct
    CbSize      DWORD   ?
    ClassGuid   GUID    <>
    Flags       DWORD   ?
    Reserved    ULONG   ?
SP_DEVICE_INTERFACE_DATA ends

Upvotes: 3

Related Questions