Analysis of Several Difficult Problems in Assembly Language Programming

Abstract

In assembly language teaching process, Some problems in assembly language programming are difficult to explain. In order to improve the quality of teaching, the best way is to verify them by experiments. This paper is an experimental verification of several main problems encountered in the teaching process, and gives a detailed explanation. This paper includes the specific allocation of memory in runtime, the calculation of transfer address in transfer instruction, the differences between hard interrupt and soft interrupt. The purpose of this paper is to provide a clear understanding of the difficult problems in assembly language programming.

Share and Cite:

Wu, W. (2019) Analysis of Several Difficult Problems in Assembly Language Programming. Creative Education, 10, 1745-1752. doi: 10.4236/ce.2019.107124.

1. Introduction

Assembly language is a programming language directly oriented to processors. The processor works under the control of instructions. Each instruction that the processor can recognize is called a machine instruction. Each processor has its own set of instructions that can be recognized, called instruction set. When the processor executes instructions, it takes different actions according to different instructions and completes different functions. It can not only change its internal working state, but also control the working state of other peripheral circuits (Wang, 2013) .

Because assembly language has the characteristic of “machine dependence”, when programmers write programs in assembly language, they can fully arrange various resources within the machine, so that they are always in the best use state. The program written in this way has short execution code and fast execution speed. Assembly, language is one of the most closely related and direct programming languages with hardware and the highest efficiency in time and space. It is one of the compulsory courses of computer application technology in Colleges and universities. It plays an important role in training students to master programming technology and familiarize themselves with computer operation and program debugging technology. This paper is the author’s teaching of assembly language course. Several difficult problems encountered here are written for your reference.

2. Assignment of Program Space in Assembler Runtime

Storage space allocation is needed when assembler runs, which is done by the operating system. When each assembler runs, it first allocates a segment prefix PSP, because DOS uses PSP to communicate with the loaded program.

PSP is 256 bytes, as shown in Figure 1. When the executable file is generated to a certain extent, the program is first transferred into memory when it is executed. At this time, the segment address of the program stored in memory is stored in DS. PSP occupies the first 256 bytes of DS:0000H segment. The contents are some instructions of the program, such as how much space the program occupies, etc. Then the real program address is the program address, and CS is pointed here, IP. Setting it to 0000, it is for this reason why CS is 10H larger than DS in general.

The following is a practical program to observe this effect.

The program of Figure 2 runs in DEBUG step by step as shown in Figure 3 and Figure 4. From the execution process of Figure 3, it can be seen that the address space of the program starts from 075A:0000, followed by the program segment prefix PSP of 256 bytes, followed by the data segment. The address space starts from 076A:0000, the data segment size is 16 bytes, then the stack segment of 16 bytes, and the address space starts from 076B:0000. The address space starts at 076C:0000. Through the above experiments, it can be clearly explained to students how the address space of an assembly language program is

Figure 1. PSP.

Figure 2. Assembly programme 1.

Figure 3. The running result 1 of assembly programme 1.

Figure 4. The running result 2 of assembly programme 1.

actually allocated in memory. For example, the following information can be obtained from the above run result graph: when a program has just been loaded into memory and has not yet started running, the DS register stores the starting address of the program segment prefix of the program, and this is the beginning address of the program segment prefix. The address is assigned by the DOS operating system. If the definition order of data segment and stack segment in the program is changed, that is to say, the green part of the above code is put in front of the red code segment, and run as shown in Figure 4. Compared with Figure 3 and Figure 4, it can be seen that after changing the order, the program runs only to change the allocation order of the address of the data segment and the stack segment, but the address allocated by the data segment and the stack segment has not changed.

3. Address Calculation of Jump Instructions

The program is shown in Figure 5. The result of this program is shown in Figure 6. As can be seen from Figure 6, the instruction mov ax, 4c00h; the function of int 21h is simply to point the IP pointer to their next instruction: start: mov ax, 0.

Using disassembly instructions, Figure 7 is obtained for the above programs. As can be seen from the figure, the physical address of label S1 is 076A:0018H, the physical address of label S2 is 076A:0020H, and the instruction machine code of instruction s2:jmp short S1 is EBF6, in which the meaning of EB is jmp, and F6 represents the jump distance of the instruction. The distance value is calculated as follows: when the CPU reads in the machine code EBF6, the content of its IP pointer points to the next instruction nop. Its physical address is 076A:0022H, and the distance from the address to the physical address 076A:0018H of the label S1 is 10 (decimal system). Because it is a jump up, its value is negative. The

Figure 5. Assembly programme 2.

Figure 6. The running result 1 of assembly programme 2.

Figure 7. The running result 2 of assembly programme 2.

complement of decimal system-10 is F6, and its address calculation is derived from 0022H + F6H = 0018H.

076A:0009 memory cells, and their machine codes are 90H, as shown in Figure 8. Then one-step running of the above program, when running to the instructions mov cs:[di], ax, as shown in Figure 9, the contents stored in the storage units 076A:0008 and 076A:0009 are programmed with EBF6, as shown in Figure 10. Continue to run instruction s0:jmp shorts, then CS and IP become 076A:0008H, then CPU reads machine code EBF6, then IP becomes 0010H. According to the above address jump calculation method, it is concluded that when machine code EBF6 is executed, IP will become 0000H, so the instruction mov ax, 4c00h, int 21h will be executed again, and when the instruction is executed again, the whole program will jump out of the node. Bundle, as shown in Figure 11. It can also be seen from the above process that when the instruction mov ax, 4c00h, int 21h is placed at the beginning of the program, its function is only to point the IP pointer to the next instruction address of this instruction, but when this instruction is executed during the execution of the program, it will cause the IP pointer to jump out of the whole program.

4. The Explanation of Hard Interrupt and Soft Interrupt

Figure 12 is an assembler program that uses keyboard and screen for input and output. The result is that the number is displayed on the screen when the number is input from the keyboard, and the “*” number is displayed when other characters are input. The results are shown in Figure 13.

Internal interruption means that the CPU does not follow the instructions that have just been executed down, but instead moves on to handle this particular information. External interruption refers to the CPU in the computer system, in addition to the ability to execute instructions and operations, but also should be able to control external equipment, receive their input and output to them. Statements in the program Mov ah, 7, int 21h belongs to function call No. 7 in DOS interrupt, which receives keyboard input information and belongs to soft

Figure 8. The running result 3 of assembly programme 2.

Figure 9. The running result 4 of assembly programme 2.

Figure 10. The running result 5 of assembly programme 2.

Figure 11. The running result 6 of assembly programme 2.

Figure 12. Assembly programme 3.

Figure 13. The running result of assembly programme 3.

interrupt. Its process is to find the entry address of interrupt program according to int 21h instruction. This interrupt program is used to read keyboard input characters, and the interrupt is triggered by int 21h instruction; instruction in al, 60H is the information read directly into port 60h, and the information of port 60H is the same as that of port 60h. Sample from the keyboard input, the reading process is still to use the same int 21h instruction pointed to the interrupt service program, but this call process is triggered by the keyboard keys caused by the changes in the keyboard internal circuit switch state, belongs to hard interrupt. The substitution of these three instructions well explains the difference and relationship between soft interrupt and hard interrupt (Wu, Wang, & Liu, 2009; Qian, 2004) .

Summary: For beginners, assembly language, because of its close combination with hardware, if their hardware knowledge is not enough, then the course will be more difficult to grasp; For teachers, some of the concepts are particularly difficult to explain clearly, This paper makes a preliminary discussion on some difficult problems in assembly language through examination, including assignment of program space, address calculation of jump instructions and explanation of hard interrupt and soft interrupt, hoping that through a few practical examples, it can be helpful to teachers and students engaged in this teaching work.

Conflicts of Interest

The authors declare no conflicts of interest regarding the publication of this paper.

References

[1] Qian, X. J. (2004). Recognition of Assembly Language. Teaching in China University, 47-49.
[2] Wang, S. (2013). Assembly Language (3rd ed.). Beijing: Tsinghua University Press.
[3] Wu, W., Wang, X., & Liu, X. Y. (2009). Teaching Reform of Assembly Language Programming. Journal of Southwest Normal University (Natural Science Edition), 34, 201-204.

Copyright © 2024 by authors and Scientific Research Publishing Inc.

Creative Commons License

This work and the related PDF file are licensed under a Creative Commons Attribution 4.0 International License.