[Assembly theory] Assembly Language

Source: Internet
Author: User
Tags decimal to binary integer division
1. Data Transmission commands ── ─ they transmit data between the storage and registers, registers and input and output ports. 1. general data transmission commands. moV transfers words or bytes. movsx first extends the symbol and then transmits it. movzx is not extended before transmission. push pushes words into the stack. pop pops up the word stack. pusha pushes ax, CX, dx, BX, SP, BP, Si, di into the stack in sequence. popa pops up the stack of DI, Si, BP, SP, BX, dx, CX, and ax in sequence. pushad pushes eax, ECx, EDX, EBX, ESP, EBP, ESI, and EDI to the stack in sequence. popad pops up the stacks of EDI, ESI, EBP, ESP, EBX, EDX, ECx, and eax in sequence. bswap swap 32-bit register byte sequence xchg swap word or byte. (at least one operand is a register, and the segment register cannot be an operand) cmpxchg compares and exchanges the operands. (The second operand must be the accumulators Al/ax/eax) xadd first swap and then accumulate. (The result is in the first operand. ── BX points to the starting point of a 256-byte table. Al indicates the index value of the table (0-255, that is, 0-ffh). The return result of Al is the table query result. ([bx + Al]-> Al) 2. input and Output port transfer command. in I/O port input. (Syntax: In accumulators, {port number │ DX}) Out I/O port output. (Syntax: out {port number │ DX}, accumulators) the input and output ports are scheduled in the immediate mode. The range is 0-255. The input and output ports are scheduled in the DX mode, the value range is 0-65535. 3. destination Address Transfer command. load Lea to a valid address. example: Lea dx, string; Save the offset address to DX. LDS transfers the target pointer and loads the pointer content into Ds. for example: lDs Si, string; Save the segment address: Offset address to DS: Si. les transfers the target pointer and loads the pointer content into es. for example, les Di and string; Save the segment address: Offset address to ES: Di. LFS transfers the target pointer and loads the pointer content into FS. for example: LFS Di, string; Save the segment address: Offset address to FS: Di. LGS transfers the target pointer and loads the pointer content into Gs. for example: LGS Di, string; Save the segment address: Offset address to GS: Di. the LSS transfers the target pointer and loads the pointer content to the SS. for example: LSS Di, string; Save the segment address: Offset address to SS: Di. 4. flag transfer command. transmit the lahf flag register and load the flag into ah. send the sahf flag register and load the ah content into the flag register. pushf marks the inbound stack. popf marks the exit of the stack. pushd 32-bit flag into the stack. popd 32-bit marks the stack. 2. arithmetic operation commands ── ─ add addition. addition of ADC incoming bits. INC plus 1. ASCII code adjustment for AAA addition. decimal adjustment of DAA addition. sub subtraction. SBB with borrow subtraction. dec minus 1. NEC reverse (minus 0 ). CMP comparison. (two operands are used for subtraction. Only the flag bit is modified and the result is not returned ). ASCII code adjustment for AAS subtraction. decimal adjustment of the DAS subtraction. mul unsigned multiplication. imul integer multiplication. if the preceding two parameters are used, AH and Al (byte operation) are returned, or Dx and ax (word operation) are used to adjust the ASCII code of AAM multiplication. div unsigned division. idiv integer division. the above two results are returned: the Al is returned by the provider, the ah is returned by the remainder (byte operation), the ax is returned by the provider, and the DX is returned by the remainder (word operation ). ASCII code adjustment for aad division. CBW byte conversion to word. (extend the byte symbol in Al to AH) convert the CWD word into a double character. (extend the word symbol in ax to dx) convert the cwde word to the double word. (extend the word symbol in ax to eax) CDQ dual-word extension. (extend the characters in eax to EDX) 3. Logic Operation commands ── ─ and operation. or operation. XOR exclusive or operation. not inverse. test. (two operands are used for operations. Only the flag bit is modified and the result is not returned ). SHL logic shifts left. sal arithmetic shifts left. (= SHL) Right Shift of the SHR logic. SAR arithmetic shifts right. (= SHR) ROL loop shifts left. ror loop shifts right. RCL shifts left through carry loop. RCR shifts right through carry loop. in the preceding eight shift commands, the number of shifts can reach 255. the operation code can be used directly when one shift occurs. for example, SHL ax, 1. if one shift is performed, the CL registers the number of shifts. for example, mov Cl, 04 SHL ax, CL 4, string commands ── ─ DS: si source string segment register: Source string address change. ES: di target string segment register: Target string address. CX repeat count counter. al/ax scan value. d sign 0 indicates that the Si and Di in the repeated operation should be automatically incremental; 1 indicates that the Si and Di should be automatically reduced. the Z mark is used to control the end of a scan or comparison operation. movs string transmission. (movsb transfer character. movsw transfer word. movsd transfers two characters .) CMPS string comparison. (cmpsb comparison character. cmpsw comparison .) SCAs string scan. compare the content of Al or ax with the target string, and the comparison result is reflected in the flag. lods Mount string. add the elements (words or bytes) in the source string to Al or ax one by one. (lodsb transfer character. lodsw transfer word. lodsd transfer dual-character .) STOs save string. is the lods inverse process. rep is repeated when CX/ECx <> 0. repe/repz is repeated when ZF = 1 or the comparison result is equal and CX/ECx <> 0. repne/repnz is repeated when ZF = 0 or the comparison results are not equal and CX/ECx <> 0. repc repeats when cf = 1 and CX/ECx <> 0. repnc repeats when cf = 0 and CX/ECx <> 0. 5. Procedural transfer instructions ── ─ 1> unconditional transfer instructions (long transfer) the call process of the JMP unconditional transfer instruction calls the RET/retf process and returns the result. 2> conditional transfer instruction (short transfer,-128 to + 127) (when and only when (sf xor of) = 1, OP1 Loop Control Command (short transfer) loop CX is not zero cycle. loope/loopz CX is not zero and the Mark Z = 1 is a loop. loopne/loopnz CX is not zero and the Mark Z is 0. jcxz CX is transferred at zero time. jecxz ECx is transferred when it is zero. 4> interrupt command int interrupt command into overflow interrupt iret interrupt return 5> Processor Control Command hlt processor pause until interruption or reset signal occurs. when the chip lead test is high, the CPU enters the waiting state. ESC to external processor. lock to block the bus. NOP null operation. STC sets the carry flag. CLC flags. the CMC carry mark is reversed. STD indicates the position in the direction. CLD flags in the clear direction. STI allows interruption. CLI clear interrupt allowed bits. 6. pseudoinstructions ── ─ DW definition (2 bytes ). set proc Process. the endp process ends. segment definition segment. assume creates block register addressing. the ends segment ends. end program ends. for beginners, the compilation of many commands is too complex, often learning for a long time can not write a beautiful program, so as to hinder our interest in learning compilation, many people give up. Therefore, I personally read the legal compilation, but I don't have to write a program. Writing a program is really not a compilation strength. You may wish to play debug, sometimes crack has a more sense of accomplishment than a program (just like learning to play games first on a computer ). Some advanced commands are actually only useful to experienced assembler programmers. They are too advanced for us. To make learning assembly language a good start, you must first exclude the gorgeous and complex commands and focus your attention on the most important commands (CMP loop mov jnz ......). However, it is easy to complete the above objectives in the textbooks of rariyo, so I have compiled this ultra-concentrated article (using WinZip, WinRAR... Oppression in turn, hey !) Tutorial. Without saying anything, read this article, and you can "inadvertently" sell debug in your predecessors or later generations. It's a great sense of accomplishment. Try it! So what about next? -Here we go! (It doesn't matter if you don't understand it when you read it. It must be broken down in the following sections.) Because assembly is used to communicate with hardware through CPU and memory, so we have to first understand the CPU and memory: (this is not to mention the number of issues) CPU is a chip that can execute all the arithmetic/logical operations and basic I/O control functions of the computer. An assembly language can only be used for a specific CPU. That is to say, the command syntax of different CPU assembly languages is also different. Since its launch in 1981, the CPU development process for personal computers is: 8086 → 80286 → 80386 → 80486 → Pentium → ......, AMD and cyrix are also supported. Later compatible with the previous CPU functions, but added some commands (such as the MMX Instruction Set with high performance) and increased the registers (such as the 32-bit eax of 386), increases the number of registers (such as 486 FS ). To ensure that the assembler program is applicable to various models, we recommend that you use the 8086 assembly language with the best compatibility. All the languages mentioned in this article are 8086 assembly languages. Registers are internal components of the CPU, so data transmission between registers is very fast. Purpose: 1. perform arithmetic and logical operations on the data in the register. 2. The address stored in the Register can point to a location in the memory, that is, addressing. 3. It can be used to read and write data to peripheral devices on the computer. 8086 has 8 8 8-bit data registers, which can form 16-bit registers: Ah & Al = AX: Accumulate registers, which are commonly used in operations; BH & BL = Bx: base Address Register, often used for address index; ch & CL = Cx: Count register, often used for counting; DH & DL = DX: data register, often used for data transmission. To use all the memory space, 8086 sets four segment registers, specifically used to save the segment address: CS (code segment): code segment register; DS (Data Segment): Data Segment register; SS (stack segment): Stack segment register; es (extra segment): additional segment register. When a program is to be executed, it is necessary to determine the locations of the memory used by the program code, data, and stack. By setting the CS, DS, and SS segments, it points to these starting locations. Generally, DS is fixed, and CS is modified as needed. Therefore, the program can be written as any size when the addressable space is smaller than 64 K. Therefore, the size of the program and its data combination is limited to 64 KB, which is why the COM file cannot exceed 64 KB. 8086 uses memory as the battlefield, registers as the military base, and accelerates work. In addition to the registers mentioned above, there are also some special functions of registers: IP (intruction pointer): instruction pointer registers, which can be used with Cs to track the program execution process; SP (Stack pointer): A stack pointer that can be used with SS to point to the current stack position. BP (base pointer): base address pointer register, which can be used as a relative base address location of SS; Si (source index): Source Address Register can be used to store source address pointers relative to DS segments; di (destination index): the destination address change register, which can be used to store the destination address change pointer relative to the es segment. There is also a flag register FR (flag register), which has nine meaningful signs, which will be described in detail later. Memory is a key part of computer operation and a place where the computer stores information during work. The memory organization has many storage locations that can store numbers, called "addresses ". 8086 the address bus has 20 bits, so the CPU has an addressing space of up to 1 MB, which is also an effective control range of Dos. 8086 can only process 16 bits of data, that is, only 0 to 64 K. Therefore, you must use segmented addressing to control the entire memory address. The complete 20-bit address can be divided into two parts: 1. segment Base Address (segment): Add four binary zeros after the 16-bit binary number, that is, a hexadecimal 0, to 20-bit binary number. You can set any 64 K segment in 1 m, it is usually recorded as a 16-bit binary number; 2. offset: directly uses the 16-bit binary number to point to any address in the base address of the segment. For example: 2222 (segment base address): 3333 (offset), the actual 20-bit address value is 25553. In addition to the above nutrition, you also need to know what dos and BIOS function calls are. Simply put, function calls are similar to Win95 API, which is equivalent to subprograms. It's enough to write assembler programs. If you don't need ms or IBM subroutines, you can't do that. (For more information about function calling, see "Computer Enthusiast" in issue 98-11 ). There are two main methods to compile an assembly language: 1. Use a compiler such as MASM or tasm; 2. Use a debugging program debug. com. Debug is actually not a compiler. Its main purpose is to correct errors in Assembler programs. However, it can also be used to write short assembler programs, especially for beginners, debug is the best entry tool. The debugging operation is easy: you only need to press debug and press enter to compile the program. The process is simple. When using the compiler, you must use a text editor, compiler, Link, exe2bin, and other programs, each program must use a series of rather complex commands to process the source program. In addition, the compiler must be used to process the source program. Many indicative statements unrelated to the instruction statement must be added for the compiler to identify, using debug can avoid many obscure program lines from the beginning. In addition to assembler, debug can also be used to check and modify memory locations, load storage and execution programs, and check and modify registers. In other words, debug is designed to bring us into touch with hardware. (8086 the usage of Common commands will be explained in each assembler. It is not possible to list all commands in length ). The a command of DEBUG can compile a simple COM file, so the program compiled by debug must start with the address 100 h (the COM file requirements. Follow me, setp by setp (Press ENTER): Enter a100; Compile from DS: 100 2. input mov DL, 1; load the value 01 H into the DL register 3. input mov ah, 2; load the value 02 h into the DL register 4. enter int 21, and call dos 21 to interrupt the 2 function, which is used to display the characters 5 loaded with DL one by one. enter INT 20, call dos 20, terminate the program, and return control to debug 6. press enter. 7. now the Assembly Language Program has been put into the memory, input g (run) 8. result: a symbol is output. Because word97 cannot display the original result, you can find a fake product. Program terminated normally we can use the U command to assemble the hexadecimal machine code (unassemble) into an assembly command. You will find that the Assembly command on the right of each line is compiled into the corresponding machine code, and 8086 is actually executed using the machine code. 1. input u100, 106 1fed: 0100 b201 mov DL, 01 1fed: 0102 b402 mov ah, 02 1fed: 0104 cd21 int 21 1fed: 0106 CD20 INT 20 debug can use the R command to view and change the register content. CS: IP Address Register, which saves the address for executing commands. 1. input r ax = 0000 BX = 0000 Cx = 0000 dx = 0000 sp = ffee BP = 0000 Si = 0000 di = 0000 DS = 1fed es = 1fed Ss = 1fed cs = 1fed IP = 0100 NV up ei pl nz Na Po NC 1fed: 0100 b201 mov DL, 01 when the program is executed from DS: 100, then when the program is terminated, debug will automatically reset the IP content to 100. If you want to make this program into an independent executable file, you can use the N command to name the program. But it must be a COM file. Otherwise, it cannot be loaded with debug. Input n smile. com; we have to tell the debug program length: The program starts from 100 to 106, so it occupies 7 bytes. We use Bx to store the high part of the length value and CX to store the low part. 2. enter RBx; check the content of the Bx register. This program has only 7 bytes, so this step can be omitted 3. enter rcX; view the content of the Cx register 4. input 7; number of bytes of the program 5. enter W; Use the W command to write the program to the (write) disk. Now, we can actually access the 8086 Assembly command. When writing assembly language programs, we usually do not directly put the machine code into the memory, but into a string of mnemonic symbols. These symbols are easier to remember than hexadecimal machine codes, this is the Assembly command. The mnemonic number, indicating the operation that the CPU should perform. That is to say, the assembler language composed of mnemonic is designed for humans, and machine language is designed for PCs. Now, let's analyze a program that can display all ASCII codes. 1. enter debug 2. enter a100 3. input mov CX, 0100; number of loading cycles mov DL, 00; load the first ASCII code, and then load the new Code mov ah, 02 int 21 Inc dl; Inc: Incremental command every cycle, add 1 loop 0105 to the value in the DL Of the data register each time. Loop: the loop command. After each loop is executed, the Cx value is reduced by 1 and jumped to the starting address of the loop 105, until CX is 0 and INT 20 4 is stopped cyclically. enter g to display all ASCII codes. When we want to display any strings, such as understand ?, You can use the dos21h to interrupt the 9h function. Enter the downstream program, save the disk, and run the following command: 1. enter a100 mov dx, 109; DS: dx = starting address of the string mov ah, 9; dos 09h function call int 21; string output INT 20 dB 'understand? $ '; The definition string has two different commands in assembly language: 1. regular commands, such as mov, are CPU commands used to tell the CPU what to do during program execution, so it will be stored in the memory in the op-code mode; 2. pseudocommands: such as DB, are the commands of the debug and other compilers to tell the compiler what to do during compilation. The dB (define byte) command is used to tell debug to put all ASCII codes in single quotes into memory. The string that uses the 9 h function must end with $. Run the D command to view the contents of the database pseudo command in the memory. 6. Input d100 1975: 0100 Ba 09 01 B4 09 CD 21 CD-20 75 6e 64 65 72 73 74 ......!. Underst 1975: 0110 61 6e 64 24 8B 46 F8 89-45 04 8B 46 34 00 64 19 and $. F .. E .. f4.d. 1975: 0120 89 45 02 33 C0 5E 5f C9-C3 00 C8 04 00 57 56. e.3. ^ _....... WV 1975: 0130 6B F8 0e 81 C7 Fe 53 8b-df 8B C2 E8 32 Fe 0b C0 K ..... S ..... 2... 1975: 0140 74 05 33 C0 99 EB 17 8b-45 0C E8 D4 97 8B F0 89 t.3 ..... E ....... 1975: 0150 56 Fe 0b D0 74 EC 8B 45-08 03 C6 8B 56 Fe 5E 5f v... T .. E .... v. ^ _ 1975: 0160 C9 C3 C 8 02 00 6B D8-0E 81 C3 Fe 53 89 5E Fe ...... K ..... s. ^. 1975: 0170 8B C2 E8 fb fd 0b C0 75-09 8B 5E Fe 8B 47 0C E8 ....... u .. ^ .. g .. now let's look at another program: Input arbitrary strings on the keyboard and display them. DB 20 indicates that debug retains 20 h of unused memory space for the buffer zone. Enter a100 mov dx, 0116; DS: dx = buffer address. The database command determines the buffer address mov ah, 0a; 0ah. Call int 21. Enter the buffer zone mov DL, 0a; because the function Ah adds a homing code (0dh is generated by enter mov ah, 02;) at the end of each string, the cursor automatically returns to the front end of the input line, in order to make the new output int 21; the string does not cover the original input string, so use the function to add one after two hours; a line feed code (OAH), so that the cursor moves to the front of the next line. MoV dx, 0118; start position of the string to be loaded mov ah, 09; 9h. The output will be stopped only when the $ symbol is encountered, so int 21 must be added at the end of the string; $, otherwise, the 9 h function will continue to display useless data in the memory at random INT 20 dB 20; Define the buffer zone to send you a sentence: Learn to assemble should not be confused. We will not talk about it. To do well, you must first sharpen your tools. Rather than a compiler, debug is a "literal interpreter". The a command of DEBUG can only convert a line of Assembly commands into machine languages and execute them immediately. The true Compiler (MASM) uses the Text Editor (edit) to create an independent. ASM text file, called the source program. It is the input part of the MASM program. MASM compiles the input ASM file into a. OBJ file, which is called the target program. The OBJ file only contains information about where each part of the program is to be loaded and how it is merged with other programs. It cannot be directly loaded into the memory for execution. Link of The Link program can convert the OBJ file into an EXE file that can be loaded into the memory for execution (execute. You can also use exe2bin to convert the qualified EXE file into a COM file (the COM file not only occupies the least memory, but also runs the fastest ). Next we will use MASM to write a program with the same functions as the first program written with debug. Use Edit to edit an smile. ASM source code file. Source program debug program prognam segment assume Cs: prognam org 100 h a100 mov DL, 1 mov DL, 1 mov ah, 2 mov ah, 2 int 21 h int 21 INT 20 h INT 20 prognam ends end comparison: 1. because MASM assumes that all values are in decimal format and debug only uses hexadecimal notation, in the source program, we must add letters representing the hexadecimal format after the numbers, for example, h indicates hexadecimal, and D indicates decimal. If it is a hexadecimal number starting with a letter, you must add 0 before the letter to indicate that it is a number, such as 0ah. 2. add five lines to the source program: prognam segment and prognam ends are paired to tell MASM and link. This program will be placed in a program segment called prognam (program name, the segment name (prognam) can be used, but its position must be fixed. Assume Cs: prognam must start with the program and be used to tell the compiler where the program is located in the CS register. End is used to tell MASM that the program ends here. org 100 h is equivalent to the a100 of debug, which is compiled from the offset. All source programs of the COM file must contain these five elements and must appear in the same order and position. Just write down this item. Next, we use MASM to compile smile. ASM. You do not need to enter the additional name. ASM when entering the MASM smile plugin. Microsoft (r) macro proceser version 5.10 copyright (c) Microsoft Corp 1981,198 8. all rights reserved. object filename [smile. OBJ]: Indicates whether to change the output OBJ file name. If not, enter source listing [NUL. LST]: whether the upload requires a list file (LST). If not, enter cross-reference [NUL. CRF]: whether the operator needs to control the file (CRF). If not, enter 50162 + 403867 bytes symbol space free 0 warning errors warning error, indicating that the compiler does not understand certain statements, generally, the input is incorrect. 0 severe errors failed is a serious error that may cause program execution to fail, usually due to a syntax structure error. If no error exists, the OBJ file can be generated. OBJ contains the compiled binary result, which cannot be loaded into the memory by DOS and must be chained (linking ). Link files (smile. OBJ) are chained to an EXE file (smile. EXE ,. 1. the input link smile connector does not need to be appended with the OBJ Microsoft (r) Overlay linker version 3.64 copyright (c) Microsoft Corp 1981,198 8. all rights reserved. run file [smile. EXE]: whether the handler changes the output EXE file name. If not, enter list file [NUL. MAP]: whether the worker needs a list file (map). If not, enter libraries [. lib]: indicates whether the library file is required. If you want to enter the file name, enter link: Warning l4021: no stack Segment segment. Because the COM file does not use the stack segment, therefore, the error message "no stack segment" does not affect the normal execution of the program. Now the EXE file has been generated. We also need to use exe2bin to convert the EXE file (smile. ex E), convert to com file (smile. com ). Enter exe2bin smile to generate the BIN file (smile. Bin ). In fact, the BIN file is exactly the same as the COM file. However, since DoS only recognizes the COM, EXE, and BAT files, the binfile cannot be correctly executed. You can rename the file or directly enter exe2bin smile. com. Now, there should be a smile. com file on the disk. You just need to enter the file name smile in the prompt symbol C:> to execute this program. Do you think it is much more troublesome to use a compiler to generate a program than debug! This is true for small programs, but for large programs, you will find its advantages. Let's run the ASCII program again in the form of a compiler to see if there are any differences. First, use Edit. com to create the ASCII. ASM file. Prognam segment; definition segment assume Cs: prognam; put the base address of the segment defined above into CS mov CX, 100 h; load cycle times mov DL, 0; load the first ASCII code, next, the new code next: mov ah, 2 int 21 h Inc dl; Inc: Increment command is loaded cyclically. The value in the data register DL is added to 1 loop next each time, run Once, CX minus 1 until CX is 0, INT 20 h prognam ends is stopped cyclically; end of segment termination; Assembly ends in the source program of assembly language, each program line contains three elements: Start: mov DL, 1; load the first ASCII code, then, the new Code identifier expression annotation is loaded cyclically in the original file to make the program easier to understand and facilitate future reference. Each line of annotation is separated. The compiler ignores the annotation and the annotation data will not appear in the OBJ, EXE, or com file. Because we do not know the address of each program line when writing the source program, we must use a symbolic name to represent the relative address, which is called an "identifier ". We usually type the identifier at the appropriate position of the appropriate row. The identifier (Label) can be up to 31 bytes, so we try to use concise text as the identifier in the program. Now, you can compile the ASCII. ASM file into ASCII. com. 1. masm ascii, 2. Link ASCII, 3. exe2bin ASCII. com. Note: When you compile your program with a compiler, typing errors, misspelled identifiers, H fewer hexadecimal numbers, and logical errors often occur. The advice a veteran of assembler often gives new people is: it is best to expect that the program he wrote will be somewhat wrong (someone else told me); if the program is executed for the first time, it will get the expected result, you 'd better check it again because it may be wrong. In principle, as long as the general logic architecture is correct and the process of finding errors in the program is more interesting than writing the program itself. When writing a large program, it is best to divide it into many modules, so that the program itself can be simple, easy to write and troubleshoot, and also make the boundaries between different parts of the program clearer, saves Compilation Time. If the reading program has something that doesn't understand, it is best to take notes on the registers, memory, and other content in the paper, and slowly draw on the paper, it will suddenly become open. Next we will write a decimal value that can be obtained from the keyboard, and convert it into a hexadecimal value, which is displayed on the screen as a "large program ". To enable 8086 to execute such a function, we must first break down the problem into a series of steps called program planning. First, use a flowchart to ensure that the entire program is logically correct (needless to say! This step is required for all languages ). This modular planning method is called "top-down program planning ". When writing a program, it starts with the smallest unit module (subroutine). When each module is completed, it is merged into a large program, the method of starting small points is called "bottom-up programming ". Our first module is binihex, which is mainly used to retrieve the binary number from the 8086 BX register and display it on the screen in hexadecimal mode. NOTE: If subprograms cannot run independently, they are normal. Binihex segment assume Cs: binihex mov CH, 4; record the converted hexadecimal digits (four digits) rotate: mov cl, 4; Use Cl as the counter, the number of shifts in the Record Register rol bx, Cl; the content of the cyclic register BX, so that four hexadecimal numbers mov Al and Bl can be processed in order; transfers the low-eight-bit BL data of Bx to Al and Al, 0fh, adds Al and 30 h to the useless bits, adds 30 h to the data in Al, and stores the data in Al CMP Al, 3ah; compared with 3ah JL printit; less than 3ah, transfer add Al, 7 h; Add the data in Al for 30 h, and store the data in Al printit: mov DL, Al; load the ASCII code into DL mov ah, 2 int 21 h dec ch; ch minus one, to zero, the zero sign sets 1 jnz rotate; jnz: When the zero sign is not set 1, jump to the specified address. That is, if not, INT 20 h is transferred; The binihex ends end of the main program is returned from the subroutine, And the ROL loop register Bx (the Bx content will be provided by the second subroutine) is used by the cyclic left shift command, to process 4 hexadecimal numbers in sequence: 1. use Cl as a counter to record the number of register shifts. 2. Move the first hexadecimal value of Bx to the rightmost. Use the and (logical "and" Operation: when the corresponding bit is 1, the result is 1, and the rest is zero) to clear unnecessary parts. The result is as follows: first, store the BL value in Al, and then use and to 0fh (00001111) to clear the four digits on the left of Al. Since the ASCII code from 0 to 9 is 30 h to 39 h, and the ASCII code from A to F is 41h to 46 h, the result is 7 h interrupted: if the content of Al is less than 3ah, the Al value is only 30 h; otherwise, the Al value is 7 h. The add command adds two expressions and stores the results in the left expression. Flag register is a separate 16-bit register with 9 flag. When some assembly commands (mostly those involving comparison, arithmetic, or logical operations) are executed, the corresponding flag position is 1 or 0, and the common signs include the zero sign (ZF), the symbol sign (SF), the overflow sign (of), and the incoming sign (CF ). The flag stores the impact on a command after it is executed. Other related commands can be used to identify the status of the flag and generate actions based on the status. CMP commands are similar to subtraction. They subtract the values of two expressions, but the content of registers or memory is not changed, but the relative flag is changed. If the Al value is smaller than 3ah, the positive and negative signs are set to 0, and the opposite is set to 1. The JL command can be interpreted as follows: if it is less than, it is transferred to the specified position. If it is greater than or equal to, it is executed downward. CMP can be used together with conditional transfer commands such as JG and JL to form the branch structure of the program. It is a common technique used to write assembler programs. The second module, decibin, is used to receive the decimal number entered by the keyboard and convert it into a binary number and place it in the Bx register for Module 1 binihex. Decibin segment assume Cs: decibin mov BX, 0; BX clear newchar: mov ah, 1; int 21 h; read a keyboard and input the symbol into Al, and display sub Al, 30 h; al minus 30 h, and the result is stored in Al. After the ASCII code is converted to the binary code JL exit, CMP Al and 9d JG exit are transferred if the value is less than 0. CBW is transferred from left to right; convert 8-bit Al to 16-bit ax xchg ax, BX; swap data mov CX, 10d in ax and BX; convert the decimal number to 10 CX Mul CX; the expression value is multiplied by the ax content, save the result in ax xchg ax, BX Add Bx, ax JMP newchar; unconditionally Transfer Exit: INT 20; return to the main program decibin ends end CBW. The actual result is: if the value in Al is positive, then Ah is filled with 00 h; otherwise, ah is filled with FFH. Xchg is usually used when you need to temporarily retain the content in a register. Of course, a subprogram (CRLF) will not cover the first input decimal number for the hexadecimal number displayed later. CRLF segment assume Cs: CRLF mov DL, 0dh; press enter ascii code 0dh into DL mov ah, 2 int 21 h mov DL, 0ah; line feed asⅱ code 0ah into Ah mov ah, 2 int 21 h INT 20; back to the main program CRLF ends end now we can combine binihex, decibin, CRLF and other modules into a large program. First, we need to slightly modify the subprograms of these three modules. Then, write a program to call every subroutine. CRLF proc near; MoV DL, 0dh mov ah, 2 int 21 h mov DL, 0ah mov ah, 2 int 21 h ret crlf endp pseudo commands similar to segment and ends, proc and endp are also paired to identify and define a program. In fact, the true role of Proc is to tell the compiler that the called program is a short-range (near) or remote (FAR ). The general program is directly called by debug, so it is returned with INT 20. The program called with the call command uses the return command ret, and RET transfers the control to the address indicated by the top of the stack, the address is put by the call command that calls this program. All modules are done, and then we can combine the subprograms to make them into the decihex segment; the main program assume Cs: decihex org 100 h mov CX, 4; the number of cycles enters CX; since the subroutine uses CX, the subroutine should call the repeat: Call decibin of the cx inbound stack; call the decimal to binary subroutine call CRLF; call the Add back and line feed subroutine call binihex; call the binary to hexadecimal notation and display the subprogram call CRLF loop repeat. The subprogram calls the call CRLF loop repeat for four times, and the operation can be performed for four consecutive mov AH and 4ch. Call dos21 to interrupt the 4C function and exit the program, the function is the same as that of INT 20 h int 21 h; but it is more widely used. If int20h cannot return, try decibin proc near push CX; press CX into the stack; pull Exit: pop CX; restore CX; retdecibin endp Bini Hex proc near push CX release pop CX retbinihex endp CRLF proc near push CX release pop CX retcrlf endpdecihex ends end call command is used to call a subroutine, and control is transferred to the subroutine address, at the same time, set the downstream command address of the call as the return address and press it into the stack. Call can be divided into two types: short-range (near) and remote (FAR): 1. Near: the IP content is pushed into the stack for the program and the program in the same segment. 2. Far: The content of CS and IP register is pushed into the stack in sequence, which is used for programs and programs in different segments. Push and pop are a pair of commands used to press the register content into and pop up to protect register data. Many subprograms are used in call. The stack pointer has a "back-to-first-out" principle, such as push ax, push BX... So that pop Bx and pop ax can protect the data. The compilation language hyper-concentration tutorial is coming to an end, hoping to lay the foundation for your independent design. More and better techniques depend on your accumulation. Wish you success!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.