A unit length of reference numerals
Previous lesson, we have to label instructions, data, start of section label use in the code segment. For example, the following program
8 data at a reference code in the accumulation section, and stores the result in the word at the numeral b.
assume cs:code
code segment
a: db 1,2,3,4,5,6,7,8
b: dw 0
start:mov si,offset a
mov bx,offset b
mov cx,8
s:mov al,cs:[si]
mov ah,0
add cs:[bx],ax
inc si
loop s
mov ax,4c00h
int 21h
code ends
end start
Program, code, a, b, start , s is a numeral. The reference numeral only indicates the address of the memory cell. We can also use one reference,
which reference numeral only indicates the address of the memory cell, said memory cell length, i.e., this label indicates the unit is at a byte unit
or a word unit, or double word units. The above program can also be written like this:
assume cs:code
code segment
a db 1, 2, 3, 4, 5, 6, 7, 8
b dw 0
start:mov si,0
mov cx,8
s:mov al,a[si]
mov ah,0
add b,ax
inc si
loop s
mov ax, 4c00h
int 21h
code ends
end start
Used in the code section a, b behind no ":", which are described simultaneously and the memory address reference unit length. Reference numeral a, describes the address
code: 0, and starting from this address, after the address of memory is byte unit; and b described with reference address code: 8, and from
a start address, after the memory cells are word units.
Because this reference contains a description of the unit length, so that in the instruction, it may represent a segment of the memory cell, for example, the program
in the "b dw 0"
指令 mov ax,b 相当于mov ax,cs:[8]
指令 mov b,2 相当于 mov word ptr cs:[8],2
指令 inc b 相当于 inc word ptr cs:[8]
For the program "a db 1, 2, 3, 4, 5, 6, 7, 8"
指令 mov al,a[si] 相当于 mov al, cs:0[si]
指令 mov al, a[3] 相当于 mov al, cs:0[3]
指令 mov al, a[bx+si+3] 相当于 mov al, cs:0[bx+si+3]
Second, the reference data used in other segments
In general, we do not define the data in the code segment, the other segment but rather is defined in the other segments, we can also use reference data to
describe the address and length of the cell for storing data.
Note that, in the behind ":" the address label can only be used in the code segment, can not be used again in the other segments.
As described in the program
assume cs:code, ***es:data***
data segment
a db 1, 2, 3, 4, 5, 6, 7, 8
b dw 0
data ends
code segment
start: ***mov ax, data
mov es, ax***
mov si, 0
mov cx, 8
s: mov al, a[si]
mov ah, 0
add b, ax
inc si
loop s
mov ax, 4c00h
int 21h
code ends
end start
Note, if you want to use the reference data in the code segment to access the data, it is necessary to assume a pseudo-instruction segment and where the label segment registers a
link. Otherwise the compiler at compile time, the label can not be determined in which a segment address register . For example, in the above procedures we
want to reference data with the data segment in a code segment, b to access the data, it must be a register and the data segment associated with assume. In the program
, we associate with ds segment register and the data segment, the compiler compiles the relevant instruction is as follows:
an instruction mov al, a [si] compiled into mov al, [si + 0]
command add b, ax compiled to add [8], ax
because the actual compilation of these instructions, by default segment address in ds access unit, whereas the actual segment is Data to be accessed, so that access to
the right, until these instructions are executed, must be as ds segment address of the data segment, then we use the program instructions:
MOV AX, data
MOV ds, AX
set point ds data section.
We can also define the data as a label, such as:
Data segment
a DB. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8
b DW 0
C DW a, b; where a and b corresponds to and offset a B offset
D dd a, B; a corresponds to the offset, SEG a, offset B, SEG B
Data ends
SEG operator, to achieve a functional segment address label
Third, the direct addressing table
Preparation of a subroutine to calculate sin (x) x∈ {0 °, 30 °, 60 °, 90 °, 120 °, 150 °, 180 °}, and displays the result in the middle of the screen
assume cs:code
code segment
start:mov al, 30
mov ah, 0
call showSin
mov ax, 4c00h
int 21h
showSin:jmp short show
table dw arg0, arg30, arg60, arg90, arg120, arg150, arg180 ;字符串偏移地址
arg0 db '0', 0
arg30 db '0.5', 0
arg60 db '0.866', 0
arg90 db '1', 0
arg120 db '0.866', 0
arg150 db '0.5', 0
arg180 db '0', 0
show:push bx
push es
push si
mov bx, 0b800h
mov es, bx
;以下用角度/30作为相对于table的偏移,取得对应的字符串的偏移地址,放在bx中
mov ah, 0
mov bl, 30
div bl
mov bl, al
mov bh, 0
add bx, bx
mov bx, table[bx]
;以下显示sin(x)对应的字符串
mov si, 160*12+40*2
shows:mov ah, cs:[bx]
cmp ah, 0
je showret
mov es:[si], ah
inc bx
add si, 2
jmp short shows
showret:pop si
pop es
pop bx
ret
code ends
end start
The results:
the program we settled through direct look-up table to ask for the location of the element to find in the table, according to data like this by directly calculate the
table to find the location of elements, we called direct addressing table, this is typical to find space for efficiency.