时间:2015-12-12 来源:互联网 浏览量:
课程名称: 汇编语言程序 实验项目名称:算术处理程序
实验类型: 验证型 年级专业: 计算机科学与技术
学生姓名: 夏 学 号:
同组学生姓名: (无) 指导老师: 王
实验地点: 实验日期:
天气状况: 晴 实验成绩:
一、实验目的要求
掌握多位数的算术运算、移位操作等程序的设计,学习使用分支与循环等基本编程方法,熟练使用DEBUG。
在数据段预先存放16个十六进制数的ASCII码,首地址为ASC。从键盘输入一位十六进制数到bx,用ASC【bx】寻址方式找到对应数位的ASCII码,并取出显示。
用16位指令编制程序,处理32位的加减乘除算术四则运算题。
AL字节内容反向排序。
二、实验原理 略
三、实验器材
Toshiba笔记本电脑一台,
虚拟机110001 夏晓光(含MASM 6.15 的windows xp系统)
四、实验步骤
【1】编制程序如下:
data segment
ASC db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
data ends
code segment
assume cs:code, ds:data
start:
MOV ax,data
mov ds,ax
mov ah,1h
int 21h
mov bl,al
mov bh,0
cmp bx,40h
jns zimu
js shuzi
shuzi: sub bx,30h
;sub dx,5
JMP JIESU
zimu: sub bx,37h
JMP JIESU
JIESU: mov cl,ASC[BX]
mov ah,4ch
int 21h
code ends
end start
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:Documents and Settingsgm??masm>masm 3
Microsoft (R) MASM Compatibility Driver
Copyright (C) Microsoft Corp 1993. All rights reserved.
Invoking: ML.EXE /I. /Zm /c 3.asm
Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000. All rights reserved.
Assembling: 3.asm
C:Documents and Settingsgm??masm>link 3
Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994
Copyright (C) Microsoft Corp 1984-1993. All rights reserved.
Run File [3.exe]:
List File [nul.map]:
Libraries [.lib]:
Definitions File [nul.def]:
LINK : warning L4021: no stack segment
C:Documents and Settingsgm??masm>
C:Documents and Settingsgm??masm>debug 3.exe
-p=0 5
AX=17BB BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17AB ES=17AB SS=17BB CS=17BC IP=0003 NV UP EI PL NZ NA PO NC
17BC:0003 8ED8 MOV DS,AX
AX=17BB BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0005 NV UP EI PL NZ NA PO NC
17BC:0005 B401 MOV AH,01
AX=01BB BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0007 NV UP EI PL NZ NA PO NC
17BC:0007 CD21 INT 21
5
AX=0135 BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0009 NV UP EI PL NZ NA PO NC
17BC:0009 8AD8 MOV BL,AL
AX=0135 BX=0035 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=000B NV UP EI PL NZ NA PO NC
17BC:000B B700 MOV BH,00
-p
AX=0135 BX=0035 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=000D NV UP EI PL NZ NA PO NC
17BC:000D 83FB40 CMP BX,+40
-p
AX=0135 BX=0035 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0010 NV UP EI NG NZ NA PE CY
17BC:0010 7907 JNS 0019
-p
AX=0135 BX=0035 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0012 NV UP EI NG NZ NA PE CY
17BC:0012 7800 JS 0014
-p
AX=0135 BX=0035 CX=0035 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0014 NV UP EI NG NZ NA PE CY
17BC:0014 83EB30 SUB BX,+30
-p
AX=0135 BX=0005 CX=0035 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0017 NV UP EI PL NZ NA PE NC
17BC:0017 EB05 JMP 001E
-p
AX=0135 BX=0005 CX=0035 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=001E NV UP EI PL NZ NA PE NC
17BC:001E 8A8F0000 MOV CL,[BX+0000] DS:0005=35
-p
AX=0135 BX=0005 CX=0035 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0022 NV UP EI PL NZ NA PE NC
17BC:0022 B44C MOV AH,4C
-p
AX=4C35 BX=0005 CX=0035 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0024 NV UP EI PL NZ NA PE NC
17BC:0024 CD21 INT 21
再次测试字母:
C:Documents and Settingsgm??masm>debug 3.exe
-p=0 8
AX=17BB BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17AB ES=17AB SS=17BB CS=17BC IP=0003 NV UP EI PL NZ NA PO NC
17BC:0003 8ED8 MOV DS,AX
AX=17BB BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0005 NV UP EI PL NZ NA PO NC
17BC:0005 B401 MOV AH,01
AX=01BB BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0007 NV UP EI PL NZ NA PO NC
17BC:0007 CD21 INT 21
F
AX=0146 BX=0000 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0009 NV UP EI PL NZ NA PO NC
17BC:0009 8AD8 MOV BL,AL
AX=0146 BX=0046 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=000B NV UP EI PL NZ NA PO NC
17BC:000B B700 MOV BH,00
AX=0146 BX=0046 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=000D NV UP EI PL NZ NA PO NC
17BC:000D 83FB40 CMP BX,+40
AX=0146 BX=0046 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0010 NV UP EI PL NZ NA PE NC
17BC:0010 7907 JNS 0019
AX=0146 BX=0046 CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0019 NV UP EI PL NZ NA PE NC
17BC:0019 83EB37 SUB BX,+37
-P
AX=0146 BX=000F CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=001C NV UP EI PL NZ AC PE NC
17BC:001C EB00 JMP 001E
-P
AX=0146 BX=000F CX=0036 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=001E NV UP EI PL NZ AC PE NC
17BC:001E 8A8F0000 MOV CL,[BX+0000] DS:000F=46
-P
AX=0146 BX=000F CX=0046 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0022 NV UP EI PL NZ AC PE NC
17BC:0022 B44C MOV AH,4C
-P
AX=4C46 BX=000F CX=0046 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17BB ES=17AB SS=17BB CS=17BC IP=0024 NV UP EI PL NZ AC PE NC
17BC:0024 CD21 INT 21
-P
Program terminated normally
观察cx的值,它的值正好是输入字符的ascii码,可知程序正确。
【2】编制程序如下:
data segment
x dw 5
y dw -3
z dw 4
v dd ?
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,3
imul x ;3*x
mov cx,ax
mov bx,dx
mov ax,y
cwd
add cx,ax
adc bx,dx
mov ax,z
cwd
sub cx,ax
sbb bx,dx
mov ax,cx
mov dx,bx
mov cx,5
idiv cx
lea bx,v
mov [bx],dx
mov [bx+2],ax
code ends
end start
将以上程序内容在记事本中编辑好,然后保存为2.asm文件。然后
C:masm>masm 2.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.
Object filename [2.OBJ]:
Source listing [NUL.LST]:
Cross-reference [NUL.CRF]:
50874 + 416198 Bytes symbol space free
0 Warning Errors
0 Severe Errors
C:masm>link 2.obj
Microsoft (R) Overlay Linker Version 3.60
Copyright (C) Microsoft Corp 1983-1987. All rights reserved.
Run File [2.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment
C:masm>debug 2.exe
-u
13A9:0000 B8A813 MOV AX,13A8
13A9:0003 8ED8 MOV DS,AX
13A9:0005 B80300 MOV AX,0003
13A9:0008 F72E0000 IMUL WORD PTR [0000]
13A9:000C 8BC8 MOV CX,AX
13A9:000E 8BDA MOV BX,DX
13A9:0010 A10200 MOV AX,[0002]
13A9:0013 99 CWD
13A9:0014 03C8 ADD CX,AX
13A9:0016 13DA ADC BX,DX
13A9:0018 A10400 MOV AX,[0004]
13A9:001B 99 CWD
13A9:001C 2BC8 SUB CX,AX
13A9:001E 1BDA SBB BX,DX
-u
13A9:0020 8BC1 MOV AX,CX
13A9:0022 8BD3 MOV DX,BX
13A9:0024 B90500 MOV CX,0005
13A9:0027 F7F9 IDIV CX
13A9:0029 8D1E0600 LEA BX,[0006]
13A9:002D 8917 MOV [BX],DX
13A9:002F 894702 MOV [BX+02],AX
13A9:0032 220B AND CL,[BP+DI]
13A9:0034 87C0 XCHG AX,AX
13A9:0036 2274E1 AND DH,[SI-1F]
13A9:0039 8B9EFEFE MOV BX,[BP+FEFE]
13A9:003D D1E3 SHL BX,1
13A9:003F D1E3 SHL BX,1
-t=0
ZF=0 SF=0 CF=0 OF= 0
AX=13A8 BX=0000 CX=0042 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=1398 ES=1398 SS=13A8 CS=13A9 IP=0003 NV UP EI PL NZ NA PO NC
13A9:0003 8ED8 MOV DS,AX
-t
ZF=0 SF=0 CF=0 OF= 0
AX=13A8 BX=0000 CX=0042 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0005 NV UP EI PL NZ NA PO NC
13A9:0005 B80300 MOV AX,0003
-t
ZF= 0 SF=0 CF=0 OF= 0
AX=0003 BX=0000 CX=0042 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0008 NV UP EI PL NZ NA PO NC
13A9:0008 F72E0000 IMUL WORD PTR [0000] DS:0000=0005
-t
ZF=0 SF=0 CF= 0 OF= 0
AX=000F BX=0000 CX=0042 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=000C NV UP EI PL NZ NA PE NC
13A9:000C 8BC8 MOV CX,AX
-t
ZF=0 SF= 0 CF=0 OF= 0
AX=000F BX=0000 CX=000F DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=000E NV UP EI PL NZ NA PE NC
13A9:000E 8BDA MOV BX,DX
-t
ZF=0 SF=0 CF=0 OF= 0
AX=000F BX=0000 CX=000F DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0010 NV UP EI PL NZ NA PE NC
13A9:0010 A10200 MOV AX,[0002] DS:0002=FFFD
-t
ZF=0 SF=0 CF=0 OF= 0
AX=FFFD BX=0000 CX=000F DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0013 NV UP EI PL NZ NA PE NC
13A9:0013 99 CWD
-t
ZF= 0 SF=0 CF= 0 OF= 0
AX=FFFD BX=0000 CX=000F DX=FFFF SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0014 NV UP EI PL NZ NA PE NC
13A9:0014 03C8 ADD CX,AX
-t
ZF= 0 SF=0 CF= 1 OF= 0
AX=FFFD BX=0000 CX=000C DX=FFFF SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0016 NV UP EI PL NZ AC PE CY
13A9:0016 13DA ADC BX,DX
-t
ZF=1 SF=0 CF=1 OF= 0
AX=FFFD BX=0000 CX=000C DX=FFFF SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0018 NV UP EI PL ZR AC PE CY
13A9:0018 A10400 MOV AX,[0004] DS:0004=0004
-t
AX=0004 BX=0000 CX=000C DX=FFFF SP=0000 BP=0000 SI=0000 DI=0000
ZF=1 SF=0 CF=1 OF= 0
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=001B NV UP EI PL ZR AC PE CY
13A9:001B 99 CWD
-t
ZF=1 SF=0 CF=1 OF= 0
AX=0004 BX=0000 CX=000C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=001C NV UP EI PL ZR AC PE CY
13A9:001C 2BC8 SUB CX,AX
-t
ZF=0 SF=0 CF=0 OF= 0
AX=0004 BX=0000 CX=0008 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=001E NV UP EI PL NZ NA PO NC
13A9:001E 1BDA SBB BX,DX
-t
ZF= 1 SF=0 CF= 0 OF= 0
AX=0004 BX=0000 CX=0008 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0020 NV UP EI PL ZR NA PE NC
13A9:0020 8BC1 MOV AX,CX
-t
ZF=1 SF=0 CF= 0 OF= 0
AX=0008 BX=0000 CX=0008 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0022 NV UP EI PL ZR NA PE NC
13A9:0022 8BD3 MOV DX,BX
-t
ZF=1 SF=0 CF=0 OF= 0
AX=0008 BX=0000 CX=0008 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0024 NV UP EI PL ZR NA PE NC
13A9:0024 B90500 MOV CX,0005
-t
ZF=1 SF=0 CF=0 OF= 0
AX=0008 BX=0000 CX=0005 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0027 NV UP EI PL ZR NA PE NC
13A9:0027 F7F9 IDIV CX
-t
ZF=1 SF= 0 CF=0 OF= 0
AX=0001 BX=0000 CX=0005 DX=0003 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0029 NV UP EI PL ZR NA PE NC
13A9:0029 8D1E0600 LEA BX,[0006] DS:0006=0000
-t
ZF=1 SF=0 CF=0 OF= 0
AX=0001 BX=0006 CX=0005 DX=0003 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=002D NV UP EI PL ZR NA PE NC
13A9:002D 8917 MOV [BX],DX DS:0006=0000
-t
ZF=1 SF=0 CF= 0 OF= 0
AX=0001 BX=0006 CX=0005 DX=0003 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=002F NV UP EI PL ZR NA PE NC
13A9:002F 894702 MOV [BX+02],AX DS:0008=0000
-t
ZF=1 SF=0 CF=0 OF= 0
AX=0001 BX=0006 CX=0005 DX=0003 SP=0000 BP=0000 SI=0000 DI=0000
DS=13A8 ES=1398 SS=13A8 CS=13A9 IP=0032 NV UP EI PL ZR NA PE NC
13A9:0032 220B AND CL,[BP+DI] SS:0000=05
ZF=0:运算结果不为零
ZF=1:运算结果为零
CF=0:没有进位或借位
CF=1:有进位或借位
SF=0:运算结果非负
SF=1:运算结果为负
OF=0:有符号运算没有溢出
OF=1: 有符号运算有溢出
编写程序如下:
assume cs:code
code segment
start:
mov al,11001101B
mov cx,8
xunhuan:
rcl al,1
rcr ah,1
loop xunhuan
mov al,ah
mov ax,4CH
int 21H
code ends
end start
五、实验结果分析
实验一中,使用js,jns似乎是一直检测sf标志位,因为在输入数字后是满足js,于是执行bx-30然后结果为正,此时自动又执行jns,即使在编辑时先判断jns再判断js也是如此。然后在bx-30又加一条dx-5,(dx提前赋为1)使得在jns执行前sf为ng即不满足jns,但是cpu仍是执行了jns,于是添加jmp命令。
观察cx的值,它的值正好是输入字符的ascii码,可知程序正确。
实验二中,结果是商为1(ax),余数为3(dx)。手算的(3*5+(-3)-4)/5=1···3
与程序的结果相同,程序正确。
实验三中,al初始值为11001101B,反向排序后应该是10110011B即B3,
观察-t
AX=B3B3 BX=0000 CX=0000 DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=17AB ES=17AB SS=17BB CS=17BB IP=000D OV UP EI PL NZ NA PO NC
17BB:000D B8004C MOV AX,4C00
可知al为B3,结果正确!
六、思考题
七、备注
对js,jns跳转后又到达哪里存在疑问。