Introduction to MASM (2) Basic Grammar of MASM

1. Variables and labels

1.1 identifier

Alphanumeric and special symbol strings starting with letters and special characters (_, $, ?, @)

1.2 Reserved words

Identifier with a specific purpose, the user cannot change its meaning

1.3 Variables

1.3.1 Four attributes

  • The offset is the byte distance relative to the segment start address (0)
  • Types include basic types such as BYTE, WORD and DWORD, as well as composite types such as struct
  • The segment address is the base address given when the program is loaded
  • The value is the actual
    content and can be changed

1.3.2 Definition

  • [Variable name] DB/DW/DD expression

      例:
      msg DB "How are you?",0 ;//共13个字节,DB表示以byte类型(1个字节)定义变量
      
      magic DB 50 dup('1','2','3')   
      		DB '1','2','3'; 共占53字节,并初始化为字符串"123123…";50表示容量;dup重复数据  
      
      DATA7  DB ?  ; 占1字节;‘?’,表示该变量的值不确定,即:该变量所表示的内存单元中的内容是不确定的,或者说是,当表达式为问号时,变量所对应的内存区中并没有存入新的值,而只是预留出了相应的存储空间;
      
      num dw 1,2,3,4,5,6,7,8,9,10 ;共20字节
                                  ;相当于C风格的 WORD num[10]={1,2,3,4,5,6,7,8,9,10};
    

1.4 Label

1.4.1 Properties

  • Segment address refers to the segment address of the segment where the instruction corresponding to the label is located
  • Offset address within segment offset address
  • There are two types: NEAR and FAR
    • NEAR type, which means that the label is used in the segment
    • FAR type means that the label can be used between segments

1.4.2 Definition: Add an identifier and a colon ":" in front of the instruction code;

For example: START: PUSH DS

2. Constant

2.1 pseudo-instruction EQU

PI EQU 31415926; similar to C language #define PI 31415926

2.2 pseudo-instruction name = expression

PIX = PI+2 //The compiler replaces name with expression in the preprocessing stage

2.3 Pseudo-instruction ORG

The starting address of the specified program starts
from 0000H by default
ORG 2000H; the specified program instructions are stored from address 2000H

2.4 Number System

Binary: 11101011B
Octal: 345Q
Decimal: 8097D, 8097
Hexadecimal: 1234H, 0EFDAH

2.5 Constant operations

+ - * \ 
AND OR XOR NOT 
SHL SHR //shift left  \  shift right
EQ NE   //equal  \ not equal
LT LE   //lower than  \lower equal小于等于
GT GE   //greater than  \greater equal大于等于

Example: MOV AL, ((11110000B AND 64) +5) GT 32 //Transfer the value of the comparison result to AL

3. Type indication

3.1 Operator PTR (forced type conversion)

MOV BYTE PTR AGE, 20 ;//Indicates the type of AGE

mov ax,bx; is to assign the value in the BX register to AX, since both are word types, there is no need to add "WORD"
mov ax,word ptr [bx]; is to set the memory address to "the value of the BX register" The data stored in the place is assigned ax. Since only a memory address is given, I don't know whether we want to assign ax to byte or word, so we need to specify it with word!

PTR的作用域只在当前语句中

3.2 Operator THIS (current address)

COUNTW EQU THIS WORD
COUNTB DB 51,96,78,90
AGAINF EQU THIS FAR  
AGAINN: MOV AL, 90

3.3操作符LENGTH,TYPE,SIZE(SIZE=LENGTH*TYPE),WIDTH

  • LENGTH
    • LENGTH variable name
    • Take out the length of the variable
  • TYPE
    • TYPE variable name or label
    • Get the type of variable name or label (the number of bytes occupied by the variable)
    • TYPE的值:BYTE:1, WORD:2, DWORD:4,NEAR:-1, FAR:-2
  • SIZE
    • SIZE variable name
    • Get the size of the variable
  • WIDTH
    • WIDTH variable name
    • Get the width of the record field

3.4HIGH,LOW

  • MOV AL, HIGH 56761; take the high byte of the constant
  • MOV AL, LOW 56761; take the low byte of the constant

3.5SEG,OFFSET

  • SEG

    • SEG variable name or label
    • Get the segment address of the segment where the variable name or label is located
  • OFFSET

    • OFFSET variable name or label
    • Take out the offset address in the segment where the variable name or label is located
  • For example:
    MOV AX, SEG COUNT //Remove the segment address of the COUNT segment and put it in the AX register
    MOV AX, OFFSET COUNT /Remove the offset address of the COUNT segment and put it in the AX register

4. Segment definition

  • END indicates the end of the entire assembly source program and gives the execution position of the first instruction ()

  • END START

    DATA 1 SEGMENTS ;一段的开始  
      name1 DB 50DUP(?)  
            DB 2,3,4,5  
    DATA 1 ENDS ;一段的结束
    
    DATA 2 SEGMENTS ;一段的开始  
       name2 DB 50DUP(?)  
    DATA 2 ENDS ;一段的结束  
    
    STACK SEGMENTS ;一段的开始  
          DB 5000 DUP(?)
    STACK ENDS ;一段的结束  
    
    CODE SEGMENT  
        ;把段与段寄存器相关连  
        ASSUME CS:CODE, SS: STACK; DS:DATA1, ES:DATA2  
    START: MOV AX,DATA1  
           MOV DS,AX  
           MOV AX,DATA2  
           MOV ES,AX  
           MOV AL,name1 ;name1隐含使用DS  
           MOV BL,name2 ;name2隐含使用ES。这里需要段超越。  
             ……
    CODE ENDS
        END START
    

5. Process definition

  • definition

    Process name PROC [FAR or NEAR]; The default is NEAR
    ……
    RET
    process name ENDP

  • transfer

    • CALL procedure name

    Example:
    call delay; call delay subroutine
    ...
    delay proc near; delay subroutine definition
    mov bx,200
    lll: mov cx,0
    ll: loop ll
    dec bx
    jne lll
    ret
    delay endp

6. Structure type

  • definition

    • 结构类型名 STRUC [对齐类型Alignment][,NONUNIQUE]
          Field1 Type1 Exp1
          Field2 Type2 Exp2
          ......
          FieldN TypeN ExpN
       结构类型名 ENDS
      

      FieldN: Variable name, it can be omitted, no variable name is accessed through the offset in the structure

  • use

    • [Variable name] Structure type name <[Field value list]>
    • The field value list is not re-initialized to use the default value, separated by ","

7. Record Type

  • Record name RECORD field[,field,…]
  • Field field name: width[=initial value expression] //width: binary digits, default to 0 without initialization
  • use
    • [Variable name] Structure type name <[Field value list]>

    • Variable name. Field name

      WEATHER RECORD A:5=10, B:2=3, C:3 ;按位,不能超过16位。
      TOR WEATHER <1, ,3>
      MOV AL, TOR.B
      

8. Macro

  • definition

     宏名 MACRO 参数 ;用相应内容替换到调用位置
          ...;宏定义体
          ENDM  ;结束定义
    
  • use

    • Macro name parameter [, parameter]
    • Such as: NAME PARA,PARA2...
  • Cancel macro

    • PURGE macro name 1, macro name 2, ...;

9. Conditional Assembly

例:SHOWCHAR MACRO X ;显示一个字符  
             IF X GE 35H  
                MOV AL, X  
             ELSE   
               MOV AL, '0'  
             ENDIF  
             MOV AH, 2  
             INT 21H  
             ENDM  

10. Simplify segment definition

.8086
.MODEL SMALL ;SMALL采用小模式内存,一个代码段,一个数据段;
		     ;TINY:都在一个段
			 ;MEDIUM:多个代码段,一个数据段
DOSSEG ;采用DOS方法排列段

.DATA ;数据段
		S1 DB 1,2,3
		S2 DW 500
		
.STACK 100H ;堆栈段,指定大小为100,通常默认为1KB

.CODE ;代码段

MAIN PROC FAR
				MOV AX,@DATA ; 数据段基址@DATA
				MOV DS,AX
				…
				RET
MAIN ENDP
		END MAIN
Simplified section directive Features Comment
.CODE [section name] Create a code snippet The section name is optional. If the section name is not given, the default section name will be used. For models with multiple code segments, a segment name should be specified for each code segment.
.DATA Create a data segment The section name is: _DATA
.DATA? Create a data segment without initial value variables The section name is: _BSS
.FARDATA [段 名] Establish remote call data segment with initial value The section name can be specified, if not specified, it will be named after FAR_DATA.
.FARDATA? [段 名] Establish remote call data segment without initial value The segment name can be specified, if not specified, it will be named after FAR_BSS.
.CONST Create a read-only constant data segment The section name is: CONST
.STACK [Size] Create a stack segment and specify the stack segment size The section name is: stack. If you do not specify the stack segment size, the default value is 1KB

11.Reference

1 http://blog.sina.com.cn/s/blog_84ee1fff0102xbeb.html
2 http://bdxnote.blog.163.com/blog/static/844423520096137403605/
3 https://www.cnblogs.com/galano/p/8721350.html
4 http://www.voidcn.com/article/p-pjmhoonz-yp.html

Guess you like

Origin blog.csdn.net/qq_45975757/article/details/108936384