Assembly language (Wang Shuang) Chapter VI

Chapter Six

6.1 Using data in the code segment

The following eight programming and data, the result in register ax, accumulated circulating manner

Want to use the cycle, we need to address in a group of contiguous memory unit, the previously mentioned data storage systems should be allowed to over-allocation of space, so we want to define in the program data processing, these data will eventually become part of the program after written executable file, the executable file is loaded into memory, these data will naturally get a storage space

assume cs:code

code segment
	
	dw 0123H,0456H,0789H,0abcH,0defH,0fedH,0cbaH,0987H
	
	mov bx,0
	mov ax,0
	
	mov cx,8
  s:add ax,cx:[bx]
  	add bx,2
  	loop s
  	
  	mov ax,4c00H
  	int 21H
  	
code ends

end

Dw is to define the meaning of font data, the code defines eight font data, accounting for 16 bytes of memory unit, since the code section, and defines the beginning of the code segment, the address of CS: 0 , CS: 2.CS: 4 ...... and so on

Debug command in the U, these data will be translated into assembly instructions, if you want the first real instruction is executed, you need to change the value of IP, this way, direct compiling, linking, and then execute, then will have problems, because IP initially 0, so the code needs to be changed

assume cs:code

code segment
	
	dw 0123H,0456H,0789H,0abcH,0defH,0fedH,0cbaH,0987H
	
    start:  mov bx,0
            mov ax,0

            mov cx,8
          s:add ax,cx:[bx]
            add bx,2
            loop s

            mov ax,4c00H
            int 21H
  	
code ends

end start

In addition to end directive tells the compiler program ends, but also can inform the compiler program entry where

How to set the CPU CS: IP to point to the first instruction to be executed, which is indicated by the description information in the executable file, the above procedure specified entry end start address offset address 10H, this information may be stored in executable file, the loader will set the basis of the information CS: IP

6.2 Use stack code segment

Using the stack, defined in the program data stored in reverse order

assume cs:codesg

codesg segment

	dw 0123H,0456H,0789H,0abcH,0defH,0fedH,0cbaH,0987H
	
	dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0	;再定义16个字型数据,程序加载后
										;取得16个字的内存空间
										;后面的程序将这段空间当作栈来使用

    start:	mov ax,cs
            mov ss,ax
            mov sp,30H	;将cs:10~cs:2F的内存空间当作栈
            			;注意栈顶的设置方法
            
            mov bx,0
            mov cx,8
          s:push cs:[bx]
            add bx,2
            loop s	;将代码段0~15单元8个字型数据入栈
            
            mov bx,0
            mov cx,8
         s0:pop cs:[bx]
            add bx,2
            loop s0	;依次出栈8个字型数据,达到逆序存放的目的
            
            mov ax,4c00H
            int 21H
            
codesg ends

end start

When we describe dw role, both said to be used to define the data, it can be said to open up the memory space, the final effect is the same, the definition of eight font data, it can be said has opened up eight words of memory space

Exercise:

Memory sequentially with 0: 0 to 0: 15 unit content rewriting data program, by transmitting data to the stack, the stack space is provided in the program

assume cs:codesg

codesg segment

	dw 0123H,0456H,0789H,0abcH,0defH,0fedH,0cbaH,0987H
	dw 0,0,0,0,0,0,0,0,0,0	;10个字单元用作栈空间
	
	start:	mov ax,cs
			mov ss,ax
			mov sp,24H
			
			mov ax,0
			mov ds,ax
			mov bx,0
			
			mov cx,8
		  s:push [bx]	;先把0:[bx]处的数据入栈
		    pop cs:[bx]	;入栈后立刻弹出存到原有数据处cs:0 cs:2等
		    add bx,2
		    loop s
		    
		    mov ax,4c00H
		    int 21H
		  
codesg ends

end start

6.3 the data, code, stack into different segments

Earlier in where we put the data, stack, code, etc. into the same segment, the so seem confusing if data, stack, space code needs more than 64KB, it exceeds the maximum capacity of a segment of the (8086 model)

So consider a plurality of storage segments each

assume cs:code,ds:data,ss:tack

data sgement
	dw 0123H,0456H,0789H,0abcH,0defH,0fedH,0cbaH,0987H
data ends

stack segment
	dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
stack ends

code segment
	 start:	mov ax,stack
            mov ss,ax
            mov sp,20H	;设置栈顶ss:sp指向stack:20
            
            mov ax,data
            mov ds,ax	;ds指向data段
            
            mov bx,0	;ds:bx指向data段中的第一个单元
            
            mov cx,8
          s:push [bx]
            add bx,2
            loop s	;将data段0~15单元8个字型数据入栈
            
            mov bx,0
            
            mov cx,8
         s0:pop [bx]
            add bx,2
            loop s0	;依次出栈8个字型数据到data段中的0~15单元中
            		;达到逆序存放的目的
            
            mov ax,4c00H
            int 21H
            
codesg ends

end start

Program, the segment name is equivalent to a label, represents the segment address, mov ax, meaning data is sent to ax the name for the segment address data segment, you can not use mov ds, data, because is not allowed to directly send a value 8086CPU the segment registers, data and stack are processed compiler for a value showing the address segment

Here named stcak, data, code just our name, CPU and will not go to the stack segment as a stack, data segment stored data even assume cs: code, ds: data, ss: tack, CPU will not so cs point code, ds Data point, these are only labels, it exists only in the source program

Arrangement according to our want CPU operation need to use the machine control instructions, assembly instructions in the source program is executed by the CPU needs the content, so that we end start position of the first instruction that CPU

by

mov ax,stack
mov ss,ax
mov sp,20H

Set ss: sp point stack: 20, so the stack space to use as a stack section

In short, how we define the contents of the CPU processing section, or as a data segment, relies entirely on the assembly instructions and assembly instructions for a specific set of registers to determine the source of the code was changed to a, data changed to b, stack instead c, start to d are possible

Published 84 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43569916/article/details/104547264