first , In order to facilitate our reverse , We choose a simple compiler development environment .
here , I choose TC2.0
TC2.0 Is an integrated development environment , It integrates the following files ：
TC.EXE Integrated compiler
EMU.LIB 8087 Simulation library , effect ： The system will be the same as 8087 Emulator connection , obtain exe file , Can only be used for program compilation .
CS.LIB Different mode runtime
, effect ： about windows There are many operation modes of the program , Normal user mode , administrator mode , Different operation modes , It may determine the different information users get , The library determines the operation mechanism of the program in different modes .
C0S.OBJ Common startup modes
MATH(C,H,L,M,S).lib Is the compiler's five storage modes , The maths.lib Yes tc2.0 The storage mode of compiler , connect .exe One of the prerequisites for .
GRAPHICS.LIB C Language graphic programming Graphics library file ：
Because I'm writing C When the graphics program of , Fonts are used , Text library and other library files , Generated exe Files must be placed with these library files , The program can run correctly , for convenience , I connected all these files to the GRAPHICS.LIB In this document , After compiling their own program , Just one exe The file is ready to run , No other documents are needed .
**OK, Our simplest integrated environment has been set up , next , We will write it here .c, Compile link to .exe file
Before our program runs , Most of the system will allocate the memory resources that our program will use , And then run our program to work , After that, the program releases the resources allocated to us .
Resource allocator -> The program we wrote -> Resource release procedure
TC2.0 In the development environment, both the resource allocation program and the resource release program are C0S.OBJ in .
C0S.OBJ The code structure in is similar ：
start; Run resource allocator call main function Run the resource release program end;
therefore , We rewrote it C0S.OBJ.
The red part is for us .c Application stack space and data storage space are equivalent to resource allocation program
The blue part is equivalent to main function call , The s： Instructions stored after this address . Equivalent to operation main function
The green part is to end our process ,cpu The right to use the cpu, Equivalent to a resource release procedure
good , Let's write one c Program compilation connects the necessary libraries and startup code .
c The procedure is as follows ：
We're useless in this main function , Because we rewrote the startup code ourselves , So is there any main Functions don't matter anymore , Our code link will automatically insert the startup code S： behind . Be treated as main function call .
ds：0X100 Memory space saved as array by default ( It took a long time to find out , The specific reason is unknown , Leave a question , Let's deal with it later )
0xb800：0000 Is the address of video memory , Put the character here and it will be displayed on the screen automatically , Company 2Byte, The structure is ( character , attribute ), That is, the character is stored in the low position , High memory property ,0x2 Is an attribute of a character , green .
A simple output KISS The program is written .
Let's try it ：
see , The first line of the program shows the green one KISS, success .
therefore , Here's the point ：( That's the point !!!) We use it windows Self contained debug Disassemble it , The following assembly instructions are obtained ：
I translated all the instructions .
0B57:0000 B84F0B MOV AX,0B4F 0B57:0003 8ED0 MOV SS,AX 0B57:0005 BC8000 MOV
SP,0080 0B57:0008 E80500 CALL 0010 ; call jazzi function 0B57:000B B8004C MOV AX,4C00
0B57:000E CD21 INT 21 ; Resource allocation resource destruction Start up procedure for 0B57:0010 55 PUSH BP 0B57:0011 8BEC MOV
BP,SP ; Save the top address of the stack before entering the function 0B57:0013 83EC02 SUB SP,+02 ; After entering the function ,int
i=0 Medium i The memory is stored in the stack with the top of the original stack as the bottom of the stack 0B57:0016 56 PUSH SI ;si In the subfunction, the median value may change , So the original si Value on the stack 0B57:0017
33F6 XOR SI,SI ;si=0 0B57:0019 C746FE0000 MOV WORD PTR [BP-02],0000 ; to i=0
0B57:001E 33F6 XOR SI,SI ;si=0 0B57:0020 EB32 JMP 0054 0B57:0022 8BDE MOV BX,SI
; Equivalent to temp=i 0B57:0024 81C30001 ADD BX,0100 ; Equivalent to temp=0x100+i 0B57:0028 8A07 MOV
AL,[BX] ; hold ds:[0x100+i] The value in is assigned to AL, Namely ‘K’'I''S''S'ascll Ten of yards ; Hex 0B57:002A 50 PUSH AX
;+++++++++++++ preservation AX Value of , Because the following code needs to be changed AX 0B57:002B 8BC6 MOV AX,SI ;ax=i 0B57:002D D1E0
SHL AX,1 ;ax=2*i 0B57:002F 99 CWD ; take ax Becomes double byte , high 16 All of them are 0 0B57:0030 050000 ADD
AX,0000 ;ax=i*2+0 0B57:0033 81D200B8 ADC DX,B800 ; Segment address of video memory 0B57:0037 8BD8 MOV
BX,AX ; hold ax Low of 16 Place bestow bx, Namely bx The initial offset address where the video memory is stored 0000 0B57:0039 8EC2 MOV ES,DX ; Assign the segment address of video memory to ES
0B57:003B 58 POP AX ;+++++++++++++ recovery AX Value of 0B57:003C 26 ES: ; change  It's just a section address 0B57:003D
8807 MOV [BX],AL ; Because there are characters in the low and middle bits of video memory , So assign the character to this memory address 0B57:003F 8BC6 MOV AX,SI
;----------------------------------- 0B57:0041 D1E0 SHL AX,1 ; Same as the above routine 0B57:0043
99 CWD 0B57:0044 050100 ADD AX,0001 0B57:0047 81D200B8 ADC DX,B800 ;ax=i*2+1
0B57:004B 8BD8 MOV BX,AX 0B57:004D 8EC2 MOV ES,DX
;------------------------------------ 0B57:004F 26 ES: 0B57:0050 C60702 MOV
BYTE PTR [BX],02 ; It's just here, isn't it 02 This value is assigned to the high bits of the memory address ,02 It means green 0B57:0053 46 INC SI ; Equivalent to i++
0B57:0054 83FE04 CMP SI,+04 ; compare si Is it 4, If it is 4 Then quit 0B57:0057 7CC9 JL 0022
; If si less than 4, Transfer to 22 place 0B57:0059 5E POP SI ; primary si Value out of stack ,si Restore the initial value 0B57:005A 8BE5 MOV SP,BP
; All the elements in the stack of the sub function are put out of the stack , The top of the stack is restored to its original position 0B57:005C 5D POP BP ; The stack bottom is restored to the original position 0B57:005D C3 RET
; Program delivery start function processing