This paper uses masM assembler under DOS to write assembly language. The assembly code introduced in this paper is 16-bit assembly, and the registers are 16-bit registers such as AX and Al, without 32-bit registers such as EAX.

Enter a DOS virtual machine with MASM installed and use the DOS operating system because you want to run 16-bit assembly. Enter PWB to open the compilation interface and start compiling.

Assembly has byte, word, Dword such type distinction, distinction method is the number of data, word is 8, Dword is 16 bits (double word), byte like 4 bits, if expressed in hexadecimal number, Word will account for two bits.

Let’s start with a question.

Try to write 8086 assembler program, count the number of characters "A" stored in 16K brother unit starting from main memory 40000H, and put the result in DX. Start: mov dx,4000h mov ds, dx mov cx, 16*1024 mov si, 0 mov dx, 0 find: mov al, [si] cmp al, 'A' jnz next inc dx next: inc si loop find ......Copy the code

Analysis:

First, assembly code without jump instructions such as loop or JMP is executed from the top down by default, meaning that the start section is immediately followed by the find section.

Since starting from 40000H, this number obviously cannot be stored in the 16-bit register, because there are 20 bits, so register indirect addressing is used here. It needs to use the DS register, but this DS register cannot be directly put into the data by default, you need to pass parameters in other registers. Meanwhile, when the DS register is initialized, the input data is 16 bits higher than the start address of the segment, that is, 4000h. You also need to initialize the pointer registers (SI, DI, or Bx, etc.) to 0. The si pointer register is used here. The address of the DS register plus the value of SI is the address of the memory we need to access. Mov al, [si] is used to pass the value of [si] to al.

In this case, CMP and JNZ denote unequal jump, i.e. jump to next. The specific realization is that CMP object 1, object 2, calculation object 1-object 2, the result is not saved, and the corresponding flag register, ZF,PF,SF,CF,OF, is modified according to the result OF operation, and then JNZ determines whether to jump according to the result OF the flag register. (I forget how to modify the flag register)

C dx is the sum of dx plus one.

Loop find applies the value in the CX register, because the cX register has been initialized to 16*1024 at the beginning. This is the immediate number, and the loop will terminate when cx is 0. So it’s going to cycle the most when it starts at 0

The first two lines of the find section use the AL register, which is an 8-bit register that holds byte, a single word. CMP BYTE PTR [SI]; CMP BYTE PTR [SI];

Now let’s look at the second question.

Stack 4096. DATA DATA BYTE 100 DUP(?).model small. Stack 4096. DATA DATA BYTE 100 DUP(? KVFF BYTE 0 code segment start: mov ax, @data mov ds, ax mov bl, 0 mov si, 0 mov cx, 100 L2: cmp DATA[si], bl JBE L3 inc si loop L2 imp L4 L3: mov bl, DATA[si] inc si loop L2 L4: mov KVFF, bl mov ah, 4ch int 21h code ends end startCopy the code

**. Model small** is a small segment of storage.

Stack 4096 Initializes the stack size. 4096 is almost enough.

.data is the data segment, in fact, you can also directly write this code into the code segment, personal habit question. Initialize DATA as an array of 100 bytes. Random, 100 for quantity, followed by DUP for loop assignment. Initialize KVFF to a BYTE type 0.

In the code segment, the code segment begins and the code ends. Start by passing in the data section. Since it starts with a., use an @ reference.

In L4, the following mov ah, 4ch, and int 21h are used to end the program. Equivalent to a RETURN in C.

Let’s move on to question number three

Try to write a program to write 55h from memory 40000h to 4BFFFh in each cell, and cell by cell comparison. If the written and read are identical, set AL to 7Eh, if there is an error, set AL to 81h. .model small .stack 4096 data segment base WORD 4000h data ends code segment start: assume ds:data, cs:code mov ax, data mov ds, ax mov dx, base mov ds, dx mov si, 0 mov cs, 0C000h mov al, 55h L1: mov [si], al inc si loop L1 mov si, 0 mov cx, 0C000h L2: cmp [si], al jnz L3 inc si mov al, 7Eh loop L2 jmp L4 L3: mov al, 81h L4: mov ah, 4ch int 21h code ends end startCopy the code

Assume here is to say which pieces of trial data are and which pieces of code are.

[si] in L2 is equivalent to ds[si], but can also be written mov dx, SEG base mov ds, dx mov si, OFFSET base

Finally to a copy of the online did not measure the output of the “hello word123456789”, the original link blog.csdn.net/springcsc19…

assume cs:code,ds:data
data segment
   str1 db 'hello world123456789$'
data ends

code segment
start:
   mov ax,data
   mov ds,ax

   mov dx,offset str1

   mov ah,09h
   int 21h

   mov ah,4ch
   int 21h

code ends
end start
Copy the code