課程名稱: 彙編語言程序 實驗項目名稱:算術處理程序
實驗類型: 驗證型 年級專業: 計算機科學與技術
學生姓名: 夏 學 號:
同組學生姓名: (無) 指導老師: 王
實驗地點: 實驗日期:
天氣狀況: 晴 實驗成績:
一、實驗目的要求
掌握多位數的算術運算、移位操作等程序的設計,學習使用分支與循環等基本編程方法,熟練使用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跳轉後又到達哪裏存在疑問。
電腦軟件相關教程