“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

X86 assembly syntax

  1. annotation
; I am a commentCopy the code
  1. Variable value and assignment (transfer instruction)
; Mov ax,2000H; Assigning hexadecimal 2000 to the hexadecimal register ax is equivalent to ax=2000H; Mov, bx,ax; Taking the value out of AX and assigning it to bx is the same thing as bx is equal to axCopy the code

The size of the data stored depends on the register used. For example, AX is a 16-bit register and can only hold up to 16 bits of data, that is, four hex bits of data

The hexadecimal data cannot start with a letter. 0 must be added before the data; otherwise, an error will be reported during compilation

  1. Function declaration

The structure is as follows:

Function name: function body ret; The closing tagCopy the code

Example:

print: ; Mov dx,offset STR mov ah, 9ch int 21h ret; Function ending tagCopy the code
  1. A function call

The key instruction call is used in the x86 architecture

X86 architecture assembly example:

call print ; Call the print function; Mov ah, 4ch int 21h print:; Mov dx,offset STR; Mov ah, 9ch; 9h: call video memory to read data int 21H RET corresponding to the offset address from dxCopy the code
  1. Definition of a string

    Cause: If you assign a string directly to a general-purpose register, the following two problems occur:

    • The character order is reversed
    • A maximum of two characters can be stored
    • Unable to get data address, cannot heap string for modification

    To solve this problem, you need to define strings in another way

    First of all: need to apply for a space in memory, can use pseudo instruction DB and DW

       db-->define byteDefine byte reads and writes one data, offset plus1Dw -->define word read and write a data, offset plus2
           
           
       dd-->define doubleWord defines a double word that reads and writes a data offset plus4
    Copy the code
    • The sample

      db 'hello' ; Take up five bytes of memory. The memory footprint of six bytes is dependent on the offsetCopy the code

    If you define numbers, use dW to take up two bytes for each number. Strings are special. Instead of two bytes for each character, the total length must be a multiple of two

  2. String fetching

    To obtain string data, first obtain the memory address corresponding to the data

    So how do I get the defined address?

    Step one: Alias the data

    str db 'hello' start: mov bx,str ; The alias stores the offset address end startCopy the code

    In the alias is offset, but light has offset address doesn’t work, also need to address, segment address + offset = actual physical address, alias default reads the paragraph from the ds register address, but we don’t assign a value to the ds register, as a result, we can’t get the correct data, because we don’t know the correct address is how many?

    So where do I get the string address?

    • Method 1: Directly from memory (debug only, real development certainly not)

    • Method 2: Use segment to wrap, segment can provide us with a segment address (positive solution)

      data segment str db 'hello' data ends ; Start: mov ax,data mov ds,ax mov bx, STR end startCopy the code