数据的引入
将数据与指令放在一起,使用dw(data word)定义数据,使用start告诉cpu执行的第一条指令。
将0123h, 0456h, 0789h, 0abch这几个数字累加。
assune cs:code
code segment
dw 0123h, 0456h, 0789h, 0abch;使用dw定义字型数据(一个字大小的数据)
start:;使用start指向程序执行的第一条指令
mov bx, 0
mov ax, 0
mov cx, 4
s:
add ax, cs:[bx]
add bx, 2
loop s
mov ax, 4c00h
int 21h
code ends
end start
栈的引用
利用栈将0123h, 0456h, 0789h, 0abch反序排列
assune cs:code
code segment
dw 0123h, 0456h, 0789h, 0abch;定义了4个字型数据,其长度为8
dw 0, 0, 0, 0;定义了4个字型数据,其长度为8,定义这个长度应该不会溢出吧....
start:
mov ax, cs
mov ss, ax
mov sp, dh;指向栈底,数据长度为16个字节,所以栈底的偏移地址为16-2
mov bx, 0
mov cx, 4
s0:
push cs:[bx]
add bx, 2
loop s0
mov bx, 0
mov cx, 4
s1:
pop cs:[bx]
add bx, 2
loop s1
mov ax, 4c00h
int 21h
code ends
end start
分段
assume cs:code, ds:data, ss:stack;分别将cs、ds、ss和标号对应起来,该标号就是段地址
;接下来定义各个段的内容
data segment
dw 0123h, 0456h, 0789h, 0abch
data ends
stack segment
dw 0, 0, 0, 0
stack ends
code segment
start:
mov ax, data;数据段对应标号的段地址
mov ds, ax
mov ax, stack;栈段对应标号的段地址
mov ss, ax
mov sp, dh;偏移地址指向栈底
;cs自动指向了start
mov bx, 0
mov cx, 4
s0:
push cs:[bx];这里也可以直接写成[bx]
add bx, 2
loop s0
mov bx, 0
mov cx, 4
s1:
pop cs:[bx]
add bx, 2
loop s1
mov ax, 4c00h
int 21h
code ends
end start