struct or class in assembly

懵懂的女人 提交于 2019-12-18 05:15:31

问题


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?


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/13450894/struct-or-class-in-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!