Assembly instruction 8086 EQU

I'm having trouble making a clear EQU directive in assembler (8086).

abc EQU xyz 

Does EQU literally swap abc when it is found in code with xyz, regardless of xyz value, value, etc.?

i.e. Can i write

 varA EQU [bp+4] mov ax, varA 

And one more question - global access EQU, that is, can I determine the EQU from the procedure and in the procedure for its use?

+5
source share
3 answers

EQU elements are not variables, they do not take up memory space:

  • When an EQU refers to a constant value, it becomes synonymous with that value. This value cannot be overwritten, even if you try it, it will not change.
  • When EQU refers to another variable, it becomes a synonym for this variable, so everything that happens with a synonym will happen with a variable.

Copy the following code into EMU8086 and run:

 .model small .stack 100h .data xyz DW 2016 ;β—„β– β– β–  ABC IS NOT A VARIABLE, IT IS abc EQU xyz ; JUST A SYNONYM FOR XYZ. pqr EQU 10 ;β—„β– β– β–  PQR IS NOT A VARIABLE, IT IS ; JUST A SNYNONYM FOR NUMBER 10. varA EQU [bp+2] ;β—„β– β– β–  BP POINTS TO GARBAGE. .code mov ax, @data mov ds, ax mov abc, 25 ;β—„β– β– β–  XYZ BECOMES 25!!!! mov pqr, 999 ;β—„β– β– β–  NO ERROR, BUT THE VALUE WILL NOT CHANGE. mov ax, pqr ;β—„β– β– β–  AX IS NOT 999, AX=10. mov si, varA ;β—„β– β– β–  GARBAGE. mov bp, sp mov si, varA ;β—„β– β– β–  DIFFERENT GARBAGE. push ax ;β—„β– β– β–  PUSH 10. call my_proc mov ax, NUMBER ;β—„β– β– β–  YES, EQUS ARE GLOBAL!!! (AX=0B9H). mov ax, 4c00h int 21h ;----------------------------------------- my_proc proc mov bp, sp mov si, varA ;β—„β– β– β–  WRONG VALUE (ANOTHER GARBAGE). mov si, [bp+2] ;β—„β– β– β–  PROPER VALUE (10). varB EQU [bp+2] mov si, varB ;β—„β– β– β–  WRONG AGAIN. NUMBER EQU 0b9h ;β—„β– β– β–  DEFINE EQU INSIDE PROCEDURE. ret my_proc endp 

In the case of [bp+2] it simply does not work, perhaps because the compiler cannot get a fixed value.

+5
source

EQU simply means equality, so abc EQU xyz , xyz must be defined earlier.

In your second example, this should look like

 %define varA [bp+4] mov ax, varA 

Then, after your code is compiled, an object dump will give

mov ax, [bp + 4]

then you can do something like

 Bubble equ varA mov bx, Bubble 

and you will get

mov bx, [bp + 4]

As a rule, all assemblers work the same way, although subtle nuances such as NASM syntactically exist require % , others do not.

+4
source

Some assemblers have reasonable macro support, which usually works internally as a preprocessor or very close to it.

Otherwise, as I wrote in the commentary, why don't you use the C preprocessor? (this is a standalone tool, you can project any text file with it, just using #define and others to expand your asm source, the rest of the content should not look like a C source, the preprocessor does not care, it treats the file like [any] text file )

You need it? I would not. I made huge code in ASM just because of my lack of experience, and the macro / preprocessor will not save me from this huge error (they will probably just make it less obvious and somewhat more bearable for a longer period of time).

While you do only small pieces of code in ASM for educational reasons or for performance / low-level things, macros / preprocessor will IMHO add an abstraction layer, obscuring the prepared instructions, so during debugging you can then ask "where did this?". I prefer to write each ASM instruction manually, knowing why I put it there, I do not want any surprises in the ASM code, it is already quite difficult to write free code in ASM.

At the same time, most of my later work at ASM was a 256B intro, so I really should have known about every byte released ... :)

+3
source

Source: https://habr.com/ru/post/1259050/


All Articles