The design method of ' subroutine ' in assembly language

Source: Internet
Author: User
Tags documentation implement

Assembly language is the most closely related to hardware in various computer languages, the most direct language, is the most efficient language of time and space, it can use all the hardware characteristics of computer and can directly control the hardware, so in the computer application system design and process control is essential. At present, the teaching use of 8086/ 8088 Assembly language System organization teaching is still the best choice. Among them, the subroutine technology is an important design method to solve the problem of repetition, the subroutine structure can simplify the writing of source program, improve the efficiency of program storage, reduce the error rate, increase the readability and maintainability of the program, And there is the use of the Organization and use of child program resources. When designing a subroutine, in addition to the necessary to consider the program call, return and complete a specific function of the instruction sequence, we must also pay attention to solve the problem with the common problems in the subroutine, namely: field protection, parameter transfer, subroutine nesting and recursive call, Write subroutine documentation, and more.

1 Field Protection

The purpose of field protection is to be able to return to the main program to continue execution after calling the subroutine. Therefore, the stack performs the necessary protection for the registers that are used in the subroutine.

1 1 Register protection because the main operation object in the assembly language program is the registers in the CPU, some of the registers that are used in both the main program and the subroutine are protected before the subroutine is used. Register protection is best done in subroutines and is recovered in subroutines, The procedure appears to be more complete. The method is to use the stack, because the instruction system in the specification of the stack instruction push and out stack instruction pops, and will automatically modify the stack pointer, as long as in the program design notice whether 8086/8088 of the stack according to the "LIFO" principle organized.

1 2 stack protection subroutines use call and return (RET) directives to implement the correct invocation and return. Because when the call command executes, the breakpoint address on the stack is the address for the subroutine to return to the main program, and you must be aware of the subroutine's type attribute when you program. That is, within a paragraph call or between paragraphs. Call within paragraph and return as near property, Deme tune Wang Yanling, and so on the design method of the neutron program in assembly language 37 use and return to the far property. 8086/ The 8088 assembler defines the properties of the call and RET directives by using subroutines that define the proc type attributes. If the subroutine you define is a far property, then the invocation and return of it must be the far property, and if the subroutine you define is a near property, Then the invocation and return of it must also be the near property. This allows the user to consider its properties only when defining a subroutine, and the properties of the call and RET directives can be determined by the assembler. In addition, you must also ensure that the indentation and popup bytes are consistent when you enter the subroutine, and if the stack access error occurs here , which will inevitably result in an error in the return address.

2 Parameter Pass

When the main program calls the subroutine, it often passes some parameters or control information to the subroutine, and after the subroutine executes, it often needs to return the result of the operation to the calling program. This kind of information transmission is called parameter transfer, and its commonly used methods are register transfer, memory fixed unit transfer, stack pass.

2 1 register passes are loaded into the predefined registers by the parameters that the main program will pass. into the subroutine and then removed for processing, this method is limited by the number of CPU internal registers, so only suitable for passing a small number of parameters of the occasion, such as some common software delay subroutine, are the use of a register to pass the cyclic counter initial value.

2 2 passes through the memory fixed unit the method is suitable for a large number of pass parameters. It is in memory to open up a specific area for passing parameters. Both the main program and the subroutine make the data exchange in the specified storage unit by prior agreement, which takes up a certain number of storage units. The disadvantage is that information is easily modified, Not conducive to modular design.

2 3 through the stack to implement the parameter pass this method is first in the main program in the parameter and parameter address into the stack, out of use in subroutines, because the stack operation does not occupy registers, and stack units can be automatically released after use, repeated use, easy to achieve data isolation and modular design. When you use this method, when the subroutine returns, These parameters are not useful and should be discarded. At this point, you can use the return instruction with immediate number to change the pointer to the value before the parameter into the stack.

3 subroutine nesting and recursive invocation

assembly language of the neutron program nesting as long as the stack space allows, is generally not subject to the nesting level. In embedded programming, we should pay attention to the protection and recovery of registers, Avoid register conflicts between each layer subroutine. The design of a recursive subroutine must ensure that each call does not break the parameters and intermediate results used in the previous call. To ensure the correctness of each call, the parameters of each call, the contents of the register and the intermediate results are saved.

4 subroutine Description Document

In general, the subroutine is to use repeatedly or provide user use, so when writing subroutine should try to use better algorithm, so that the subroutine run faster, and save memory. At the same time, it is also necessary to meet the needs of future program maintenance and use. The corresponding documentation should be established while designing the subroutine, Describes the functions and calling methods of subroutines clearly. Usually the subroutine description document should include: subroutine name, subroutine function, entry parameter, export parameter, work register, work unit and last modification date, etc.

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.