Wang Shuang's "Assembly Language" Personal Thoughts on How to Get the Length of a String in Programming and Handling Interrupt 0

In order to explore the interrupt, we try to write a do0 program when we learn here, and also write an installation program p247, install do0 into the system, and replace the original No. 0 interrupt routine. The complete program implementation of do0 and p247 has been given in the textbook.

But after reading the program in the textbook, I thought of a question. In the do0 given in the textbook, we count the number of characters and assign them to cx. Can the program be automatically calculated? The answer is yes.

Let's put do0 aside first. In p247, we need to count the length of do0 itself. The textbook gives us a way to count the length of do0 itself, namely:

After the end of do0, add a label do0end, and then you can  get the length of do0 itself through offset do0end - offset do0

Then, we should be able to use the same method to let the compiler count the length of the string

The implementation method is as follows:

sb:
db "chufayichu!"
sbend:
nop

Then we can assign a value to cx like this:

mov cx,offset sbend-offset sb


The complete code (do0 installer) is as follows:

assume ss:stack,cs:code

stack segment stack
	db 256 dup (0)
stack ends

segment code
main:
	mov ax,cs
	mov ds,ax
	mov si,offset do0; ds:si points to the source address
	
	mov ax,0
	mov is,ax
	mov di,200h; set es:di to point to the destination address
	
	mov cx,offset do0end-offset do0; set cx as the transfer length
	cld; set the transfer direction to positive
	rep movsb
	
	mov word ptr es: [0 * 4], 200h
	mov word ptr es:[0*4+2],0; set interrupt vector table
	
	mov ax,4c00h
	int 21h
	
		do0:
			jmp short do0start
			sb:
			db "chufayichu!"
			sbend:
			nop
			
			do0start:
			mov ax,cs
			mov ds,ax
			mov si,202h; set ds:si to point to string
			
			mov ax,0b800h
			mov is,ax
			mov di,12*160+36*2; set es:di to point to the middle of the video memory space
			
			mov cx,offset sbend-offset sb; set cx to string length
			s:
			mov al, [si]
			mov is:[di],al
			inc si
			add di,2
			loop s
			
			mov ax,4c00h
			int 21h
		do0end:
			nop
		
code ends

end main


Then we test, where "p247_1" is the do0 installer modified with the above method, and "shiyan" is a program that intentionally causes a division error to cause interrupt 0


It can be seen that before installing do0, executing the "shiyan" program will cause the original system interrupt No. 0,

And after executing p247_1 (that is, installing do0), the "shiyan" program calls the new interrupt routine No. 0, and displays our custom string "chufayichu" in the middle of the screen


That is to say, it is feasible to use the label and offset to count the length of the string in the interrupt routine.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325374984&siteId=291194637