Source program of ch3_1 assembly language program

mark, I returned to study from Hubei on August 15, 2023, and participated in an academic conference. It seems that academics do require communication, especially for many exchanges in this field, you still need to attend an academic conference at least once a year. It will not let you down

. I am too ignorant and limited to my own narrow field. I want to look more at the current progress in academia.

1. Source program written in assembly language

1.1 The working process of writing programs in assembly language

Assembler: Text containing assembly instructions and directives'

  • Assembly instructions, corresponding to instructions with machine code, can be compiled into machine instructions and finally
    executed by the CPU.

  • Pseudo-instructions
    are instructions that do not have corresponding machine codes and are ultimately not executed by the CPU.

:Who will execute the pseudo-instructions?
Pseudo-instructions are instructions executed by the compiler, and the compiler performs related compilation work based on the pseudo-instructions.

Insert image description here

assume cs:codesg
codesg segment
mov ax,0123H
mov bx,0456H
add ax,bx
add ax,ax
mov ax,4c00h
int 21h
codesg ends
end

Program return (routine!): After the program finishes running, control of the CPU is returned to the program that enabled it to run (usually a DOS system)

1.2 Three pseudo-instructions in the program

  • assume

; The meaning is to assume that a certain segment of the register
is associated with a segment in the program defined with segment... ends - assume cs:codesg means that the CS register is associated with codesg, and the
defined codesg are used as the code segment of the program.

  • segment definition

; An assembler is composed of multiple segments, which are used to store code, data or as stack space.
; A meaningful assembler must have at least one section, which is used to store code.
; Define segments in the program: each segment needs a segment name

Segment name segment - the beginning of the segment
...
segment name ends - the end of the segment

  • end (not ends)

The end marker for the assembler. If end is not added at the end of the program, the compiler cannot know where the program ends when compiling the program.

assume cs:codesg
codesg segment
mov ax,0123H
mov bx,0456H
add ax,bx
add ax,ax
mov ax,4c00h
int 21h
codesg ends
end

1.3 The source program becomes machine code after compilation and connection.

Insert image description here

1.4 Structure of assembler

  • An assembly program written by directly writing instructions in Debug
    ; suitable for short and concise programs with simple functions
    ; only needs to include assembly instructions

Insert image description here

  • A program that is written separately into a source file and then compiled into an executable file
    ; suitable for writing large programs
    ; it needs to include assembly instructions and pseudo-instructions to guide the work of the compiler
    ; the source program consists of some segments, which store code and data
    data, or use a segment as stack space
assume cs:code,ds:data,ss:stack
data segment
dw 0123H,0456H,0789H,0abcH,0defH
data ends
stack segment
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
stack ends
code segment
mov ax,stack
mov ss,ax
mov sp,20h ;设置栈段
mov ax,data
mov ds,ax ;设置数据段
mov bx,0
mov cx,8
s: push [bx]
add bx,2
loop s
....
code ends
end

; Semicolon, used to represent -- comment

1.5 How to write a program?

Example: Programming to find 2^3.
① Define a segment
② Implement the processing task
③ Indicate where the program ends
④ Associate the segment with the segment register
⑤ Add the code returned by the program

Insert image description here

1.6 Possible errors in the program

Syntax errors
; errors found by the compiler when the program is compiled;
; errors in the following programs are easy to find

aume cs:abc
abc segment
mov ax,2
add ax,ax
add ax,sx
end

Logic errors ; errors that occur
during runtime that cannot be shown when the program is compiled ; ; errors in the following programs are not easy to find

assume cs:abc
abc segment
mov ax,2
add ax,ax
add ax,bx
mov ax,4c10H
int 21H
abc ends
end2^

2. From source program to program execution

2.1 The process from writing the source program to executing the executable file

Insert image description here

2.2 Edit source program

Insert image description here

2.3 Compilation

Insert image description here

  • The object file (*.OBJ) is the final result we want to get when compiling a source program
    .

  • The list file (*.LST) is the intermediate result generated by the compiler when it compiles the source program into an object file.

  • Cross-reference files (*.CRF), like list files, are intermediate results generated by the compiler when compiling source programs into target files.

  • The compilation of the source program is completed, and the last two lines of the compiler output tell us that the source program has no warning errors and errors that must be corrected.

Insert image description here

Two types of errors
û Severe Errors
û The given
source program file cannot be found.
Add ; after the command to simplify the process

2.4 Connection

  • An executable file (.EXE) is the final result we get from connecting a program.

  • The image file (.MAP) is the intermediate result produced by the linker when linking the target file into an executable file.

  • The library file (.LIB) contains some callable subprograms. If our program calls a subprogram in a certain library file, we need to connect this library file to our target file when connecting. Together, an executable file is generated.

  • no stack segment, a "no stack segment" warning error, you can ignore this error.

: Errors may be encountered during the connection
: Example: object nor found - object not found

Insert image description here

2.5 Execute executable programs

Insert image description here

Our program does not output any information to the monitor. The program just does some operations of sending data into registers and adding, and we can't
see these things on the display.

:After the program execution is completed, return and the operating system prompt will appear on the screen again.

2.6 Summary

Source file
.asm
object file
.obj
object file
.obj
executable file
.exe

Insert image description here

3. Use Debug to track the execution of the program

Insert image description here

3.1 Load the program with Debug

Insert image description here

summary

After the program is loaded, DS stores the segment address of the memory area where the program is located. The offset address of this memory area is 0, so the address of the memory area where the program is located is: DS:0.

这个内存区的前256 个字节存PSP,DOS用来和程序进行通信。

The space from 256 bytes backward stores the program, and the value of CS is DS+10H.
; After the program is loaded, the length of the code (bytes) is stored in CX.

Where is the program loaded into memory?

Insert image description here

3.2 Use Debug to single-step the program

Insert image description here

3.3 Other methods of execution

Insert image description here

  • Continue command P (Proceed):
    similar to the T command, execute instructions one by one and display the results. But when encountering a subroutine, interrupt, etc., it is executed directly and then the result is displayed.

  • Run command G (Go): Start running the program from the specified address until a breakpoint is encountered or the program ends normally.

3.4 Different ways of program execution

  • Execute in DOS

The "normal" state of program execution

  1. After DOS starts, the computer is controlled by the "command interpreter" (the program command.com);
  1. When running an executable program, command loads the program into memory and sets the CS:IP of the CPU to point to the first instruction of the program (i.e., the entry point of the program), so that the program can be run.
  1. After the program is finished running, it returns to the "command interpreter" and the CPU continues to run the command.
  • Execute in Debug

Program execution runs in the development cycle;
Insert image description here

When running Debug, the command program loads Debug.exe, and debug loads the program into memory. After the program is finished running, you need to
return to Debug. Use the Q command to exit Debug and you will return to command.

Insert image description here

Book chapter correspondence

Insert image description here

Guess you like

Origin blog.csdn.net/chumingqian/article/details/132515807