Wang Shuang "assembly Language" (third edition) Experiment 8 analysis (Super detailed)

Source: Internet
Author: User

Topic

Analysis program, before running to think: whether this program can return correctly?
After running, think again: why is this result?
Through this program to deepen the understanding of the relevant content.

Paste in the code as follows:

assueme cs:codesgcodesg segment        mov ax,4c00h        int 21hstart:  mov ax,0    s:  nop        nop        mov di,offset s        mov si,offset s2        mov ax,cs:[si]        mov cs:[di],ax    s0: jmp short s    s1: mov ax,0        int 21h        mov ax,0    s2: jmp short s1        nopcodesg endsend start
Analysis

From the knowledge we learned before, we know that the program starts with the field of the start label.

Let's take a look at the program execution flow:

 1. Start:mov ax,0 2. S:nop;       The NOP label statement, which allocates a byte of space in the code snippet at run time, 3. NOP; The value of this byte (space) is 90h.; The function of the operator ' offset ' is to obtain the offset address of the label. 4. mov Di,offset s;       Save the offset address of S to the DI Register 5. mov si,offset S2;       The offset address of the S2 is stored in the SI register 6. mov Ax,cs:[si]; This line is to store the machine code in CS:[SI] memory in the AX register; This machine code is compiled by the compiler for the instructions in the S2 designator field. 7. mov Cs:[di],ax; The machine code for the S2 label field in AX is stored in the S label field. 8. s0:jmp Short S; Jump to the S-label field to execute the code. 9. s:jmp short S1; According to our previous analysis, the instructions are represented by relative offsets; So the operation does not really jump to s1 this label; Instead, jump to the offset of the S1 designator determined at compile time.; So we have to analyze the process of the next procedure, we must first compile the program; You know what the offset is by looking at the machine code of this instruction. Then, based on this offset, determine where the program should execute the next instruction.; Based on the compiled results, it can be found that; JMP short S1 is compiled with the following instructions: EB F6; From the above, the offset is: F6; The offset is expressed by the complement, by the note two in the book; We can figure out that the F6 corresponds to a signed decimal number of-10.; From here, we can know that this instruction is to add the IP value-10.;     So, let's see what the address ip-10 points to? ; By the result of the compilation, we can know that; It points to exactly where code segment started.   Ten. mov ax,4c00h 11. int 21h; See these two sentences, you will know that the program is able to return to the normal
Anti-compilation

Note that the debug command is used to disassemble u the code snippet by specifying the offset address of 0, or debug will automatically begin disassembly from the start label.

Sometimes you simply u 0 can't see jmp short s1 This code from the command. Because some command-line tools may not be able to display too much code. We can find a reference point from which to use the u command again, we can see this line of code. From here, we can see that jmp short S1 corresponds to the machine code, exactlyEB F6

    • The jump principle analysis of jmp short S1:
1. codesg segment        mov ax,4c00h     ; 3字节        int 21h          ; 2字节2. start:  mov ax,0         ; 3字节3.    s:  jmp short s1     ; 2字节       ; 上述4条指令总共加起来是10字节,即 10 个单位的偏移量!    ; 由于 nop 只占一个字节 , 因此    ; 原来 s 中的两个 nop 被jmp short s1完全替代。    ; CPU首先读取这条指令到指令缓存器里    ; 此时的ip为8(由可以知道)    ; 【文末的参考文章中的len(EB F6)解释错误,应为2,此处已更正】    ; 接下来 , (ip) = (ip) + len(EB F6) = (ip) + 2 = 10     ; 然后执行这条指令 , 即为 (ip) = (ip) - 10 = 0    ; 这样 ip 就回到了 code segment 的起始处    ; 这样继续执行4. mov ax,4c00h5. int 21h    ; 这样,程序就这样神奇的执行成功啦!!!
Summarize

Using the Wang Shuang teacher on the P179 page, the CPU does not need to transfer the destination address when executing the JMP instruction, but contains the shifted displacement. This displacement is calculated by the compiler according to the "label" in the assembly instruction.

This blog has reference to his article:
Links: https://www.jianshu.com/p/7e5dfea72b65

Wang Shuang "assembly Language" (third edition) Experiment 8 analysis (Super detailed)

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.