Concise x86 assembly Language Tutorial (6)

Source: Internet
Author: User
Tags variables

4.0 using subroutines with interrupts

have mastered the assembly language? Yes, you can now decipher the secret in someone else's code. However, one of the important things we did not mention was that the program and the interruption. These two things are so important that your program is almost impossible to leave them.

4.1 Sub Program

In high-level languages we often use subroutines. In advanced languages, subroutines are so magical that we can define variable names like the main program, or other subroutines, and access different variables, and not conflict with other parts of the program.

Unfortunately, however, this "advantage" does not exist in assembly language.

Assembly language does not pay attention to how to reduce the burden on programmers; Instead, assembly language relies on the good design of programmers in order to maximize CPU performance. Assembler is not a structured language, so it does not provide direct "local variables". If "Local variables" are required, they can only be implemented by the heap or stack itself.

In this sense, the subroutine of assembly language is more like those "subroutines" called by GoSub in Gwbasic. All "variables" (essentially, memory and registers that belong to the process) are shared by the entire program, and the actions of the high-level language compiler, which put local variables on the heap or stack, can only be implemented on their own.

The passing of parameters is done by registers and stacks. In advanced languages, subroutines (functions, procedures, or similar concepts) depend on the heap and stack to pass.

Let's briefly analyze the execution of subroutines in general high-level languages. Whether C, C + +, Basic, Pascal, this part of the basic is consistent.

调用者将子程序执行完成时应返回的地址、参数压入堆栈 子程序使用BP指针+偏移量对栈中的参数寻址,并取出、完成操作 子程序使用RET或RETF指令返回。此时,CPU将IP置为堆栈中保存的地址,并继续予以执行

There is no doubt that the stack plays a very important role throughout the process. However, in essence, the most important thing for a child program is to return the address. If the subroutine does not know this address, then the system will crash.

The instruction of the calling subroutine is call, and the corresponding return instruction is ret. In addition, there is a set of instructions, enter and leave, that can help with the maintenance of the stack.

The parameter of the call instruction is the address of the invoked subroutine. When using a macro assembly, this is usually a label. Call and RET, as well as enter and leave pairing, enable automatic operation of the stack without requiring programmers to push/pop and jump operations, thereby increasing efficiency.

As an implementation instance of a compiler, I compiled a section of C + + program code with Visual C + +, which is the result of using a specific compilation option, and the normal release code is much simpler. Some of the disassembly results containing the source code are as follows (from the results of the operation of the Visual C + + debugger, I removed 10 int 3 instructions and added some comments, except for any changes):

1:int mytransform (int ninput) {
00401000 push EBP; Protect the original EBP pointer on the scene
00401001 mov Ebp,esp
2:return (ninput*2 + 3)% 7;
00401003 mov eax,dword ptr [ninput]; Take parameters
00401006 Lea Eax,[eax+eax+3]; Lea is faster than add addition
0040100A CDQ; Dword->qword (extended word length)
0040100B mov ecx,7; Divisor
00401010 Idiv eax,ecx; Except
00401012 mov eax,edx; Merchant->eax (Save return value in EAX)
3:}
00401014 pop ebp; Restore the EBP pointer to the scene
00401015 ret; Return
; 10 int 3 Instructions are removed here, which are convenient for debugging and do not affect program behavior.
4:
5:int Main (int argc, char* argv[])
6: {
00401020 push EBP; Protect the original EBP pointer on the scene
00401021 mov Ebp,esp
00401023 Sub esp,10h; To fetch the ARGC, argv fix the stack pointer.
7:int A[3];
8:for (register int i=0; i<3; i++) {
00401026 mov dword ptr [i],0; 0->i
0040102D jmp main+18h (00401038); Judging the cyclic conditions
0040102F mov Eax,dword ptr [i]; I->eax
00401032 add eax,1; EAX + +
00401035 mov dword ptr [I],eax; Eax->i
00401038 cmp DWORD ptr [i],3; Cyclic conditions: I and 3 comparison
0040103C jge main+33h (00401053); If the condition is not met, the loop should end
9:a[i] = Mytransform (i);
0040103E mov Ecx,dword ptr [i]; I->ecx
00401041 push ecx; ECX (i)-> stack
00401042 call Mytransform (00401000); Call Mytransform
00401047 add esp,4; Esp+=4: New cells in the heap
; Prepare to store return results
0040104A mov Edx,dword ptr [i]; I->edx
0040104D mov dword ptr a[edx*4],eax; Will EAX (Mytransform return value)
; Put back A[i]
10:}
00401051 jmp main+0fh (0040102f); Calculate i++, and continue looping
11:return 0;
00401053 XOR Eax,eax; The return value should be 0
12:}
00401055 mov esp,ebp; Recovery stack pointer
00401057 pop ebp; Recovery bp
00401058 ret; Back to the caller (c + + running environment)

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.