微机原理习题4答案

习题4:

请编写完整汇编程序:

1.内存中以BUF单元开始存放8个16位二进制数,试编程将8个数倒序后存

放于BUF开始的单元,试编程;(提示:采用堆栈实现)

DATA SEGMENT

ORG 0000H

BUF DW 1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H COUNT EQU ($-BUF)/2

DATA ENDS

STACK1 SEGMENT STACK

DW 256 DUP(0)

STACK1 ENDS

CODE SEGMENT

ASSUME CS:CODE,SS:STACK1,DS:DATA

START: MOV AX,DATA

MOV DS,AX

MOV AX,STACK1

MOV SS,AX

LEA SI,BUF

MOV CX,COUNT

LOP1: MOV AX,[SI]

PUSH AX

INC SI

INC SI

LOOP LOP1

LEA SI,BUF

MOV CX,COUNT

LOP2:

POP AX

MOV [SI],AX

INC SI

INC SI

LOOP LOP2

CODE ENDS

END START

2.将8个16位无符号数相加,结果保存在32位无符号数SUM中;

DATA SEGMENT

BUF DW 1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H COUNT EQU ($-BUF)/2

SUM DD 0

DATA ENDS

STACK1 SEGMENT STACK

DW 100 DUP(0)

STACK1 ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA,SS:STACK1

START: MOV AX,DATA

MOV DS,AX

LEA BX,BUF

MOV CX,COUNT

MOV DX,0000H

LOP: MOV AX,[BX]

ADD WORD PTR SUM,AX

ADC WORD PTR SUM+2,DX

INC BX

INC BX

LOOP LOP

MOV AX,4C00H

INT 21H

CODE ENDS

END START

3.以十进制形式在计算机屏幕上显示内存中的一个8位有符号数,例如:若内

存单元中存放的数据为7FH,则在屏幕上显示+127,若内存单元存放的数据为0FFH,则应在屏幕上显示-1;

DATA SEGMENT

VAR DB 0FFH

STR1 DB 'THE RESULT IS: $'

DATA ENDS

SS_SEG SEGMENT STACK

DW 100 DUP(0)

SS_SEG ENDS

CODE SEGMENT

ASSUME CS:CODE, DS:DATA, SS:SS_SEG

START:

MOV AX, DATA

MOV DS, AX

LEA DX,STR1

MOV AH,09H

INT 21H

MOV DL, '+'

CMP VAR, 0

JGE NEXT

NEG VAR

NEXT: MOV AH,02H

INT 21H

MOV AL, VAR

MOV BL, 10

MOV CL,0

LOP1: AND AH,0

DIV BL

PUSH AX

INC CL

CMP AL,0

JNZ LOP1

LOP2: POP AX

MOV DL,AH

ADD DL,30H

MOV AH,2

INT 21H

LOOP LOP2

MOV AX, 4C00H

INT 21H

CODE ENDS

END START

4.从键盘输入一个4位十进制数,然后以16进制形式显示在屏幕上,试编程;

例如:输入1 0 2 4 在屏幕上应该显示0400H

DATA SEGMENT

STR1 DB 'INPUT DATA: $'

BUF DB 20

DB 4

DB 4 DUP(?)

STR2 DB 0AH,0DH,'THE RESULT IS: ','$'

DATA ENDS

SS_SEG SEGMENT STACK

DB 100 DUP(0)

SS_SEG ENDS

CODE SEGMENT

ASSUME CS:CODE, DS:DATA, SS:SS_SEG

START:

MOV AX, DATA

MOV DS, AX

LEA DX,STR1

INT 21H

MOV AH,0AH

LEA DX,BUF

INT 21H

MOV CX,03H

LEA SI,BUF+2

AND BX,0H

MOV DL,0AH

LOP1: MOV AL,[SI]

SUB AL,30H

PUSH CX

LOP2: MUL DL

LOOP LOP2 POP CX

ADD BX,AX

INC SI

LOOP LOP1

AND CH,00H

MOV CL,[SI]

SUB CL,30H

ADD BX,CX

LEA DX,STR2

MOV AH,09H

INT 21H

MOV AX,BX

AND CH,00H MOV CL,04H

MOV DH,04H

MOV DL,00H

AAA1: AND AX,000FH

PUSH AX

DEC DH

INC DL

SHR BX,CL

MOV AX,BX

CMP DH,0

JA AAA1

MOV CL,DL

BBB: POP DX

CMP DL,09H

JB NEXT

ADD DL,07H

NEXT: ADD DL,30H

MOV AH,2

INT 21H

LOOP BBB

MOV DL,'H'

MOV AH,02H

INT 21H

MOV AX,4C00H

INT 21H

CODE ENDS

END START

5.数据段中存放有一个无符号字数据VAR,将其转换成非压缩格式的BCD码,

存于BUF开始的单元中(高位在前);

例如:若VAR为0800H,则转换后(BUF) = 20H (BUF+1) = 48H

DATA SEGMENT PARA

VAR DW 0800H

BUF DB 2 DUP(0)

DATA ENDS

SS_SEG SEGMENT STACK

DW 100 DUP(0)

SS_SEG ENDS

CODE SEGMENT PARA

ASSUME CS:CODE, DS:DATA, SS:SS_SEG

START:

MOV AX, DATA

MOV DS, AX

MOV CX, 16

MAIN1: SHL VAR, 1

MOV BX, 4

PUSH CX

MOV CX, 5

MAIN2: MOV AL, BUF[BX]

ADC AL, AL

AAA

MOV BUF[BX], AL

DEC BX

LOOP MAIN2

POP CX

LOOP MAIN1

EXIT: MOV AX, 4C00H

INT 21H

CODE ENDS

END START

6.内存中以str1和str2开始分别存放了两个字符串,结束符为NULL(ASCII

码为0),将str2连接到str1后,形成1个字符串,并将连接后的字符串str1输出到屏幕上;

DATA SEGMENT

STR1 DB 'GOOD MORNING,',00H

STR DB 50 DUP(0)

STR2 DB 'Mr WANG!',0AH,0DH,00H

DATA ENDS

SS_SEG SEGMENT STACK

DW 100 DUP(0)

SS_SEG ENDS

CODE SEGMENT

ASSUME CS:CODE, DS:DATA, SS:SS_SEG

START:

MOV AX,DATA

MOV DS,AX

LEA SI,STR1

MOV AL,[SI]

CMP AL,00H

JE JP1

JP2: INC SI

MOV AL,[SI]

CMP AL,00H

JA JP2

JP1: MOV CX,01H

LEA BX,STR2

MOV AH,[BX]

MOV [BX],AH

CMP AH,00H

JE JP3

MOV [SI],AH

JP4: INC SI

INC BX

INC CX

MOV AH,[BX]

MOV [SI],AH

CMP AH,00H

JA JP4

JP3: INC SI

MOV [SI],BYTE PTR '$'

LEA DX,STR1

MOV AH,09H

INT 21H

MOV AX,4C00H

INT 21H

CODE ENDS

END START

7.统计10个有符号字节数中,大于0、小于0、等于0的个数,分别存放在

NUM1、NUM2、NUM3三个变量中,并找出最大值、最小值分别存放到MAX、MIN 变量中,再求10个数的和,将结果存放到16位有符号数SUM中。

DATA SEGMENT

NUM DB 0F0H,03H,0B4H,0AH,0AAH,00H,80H,7FH,99H,21H

COUNT EQU ($-NUM)

ORG 0010H

NUM1 DB 0

NUM2 DB 0

NUM3 DB 0

MIN DB 0

MAX DB 0

SUM DW 0

DATA ENDS

SS_SEG SEGMENT STACK

DW 100 DUP(0)

SS_SEG ENDS

CODE SEGMENT

ASSUME CS:CODE, DS:DATA, SS:SS_SEG

START:

MOV AX,DATA

MOV DS,AX

MOV SS,AX

LEA SI,NUM

MOV CX,COUNT

MOV BX,0000H

MOV DX,0000H

LOP: MOV AL,[SI]

INC SI

CMP AL,0

JG DA

JL XIAO

JE DENG

DA: INC BH

JMP AAA1

XIAO: INC BL

JMP AAA1

DENG: INC DH

AAA1: LOOP LOP

LEA SI,NUM1

MOV [SI],BH

LEA SI,NUM2

MOV [SI],BL

LEA SI,NUM3

MOV [SI],DH

MOV CX,COUNT-1

MAIN1: LEA BX,NUM

PUSH CX

MAIN2: MOV AL,[BX]

INC BX

CMP AL,[BX]

JLE NEXT

XCHG AL,[BX]

MOV [BX-1],AL NEXT: LOOP MAIN2

POP CX

LOOP MAIN1

LEA SI,MIN

MOV AL,NUM

MOV [SI],AL

LEA SI,MAX

MOV BL,NUM+9

MOV [SI],BL

MOV SI,OFFSET NUM MOV CX,COUNT

MOV AX,0

LOP2: AND BX,0

MOV BL,[SI]

ADD AX,BX

INC SI

LOOP LOP2

LEA SI,SUM

MOV [SI],AX

MOV AX,4C00H

INT 21H

CODE ENDS

END START

8 若程序的数据段定义如下,写出各指令语句独立执行后的结果。DSEG SEGMENT

DATA1 DB 10H, 20H, 30H

DATA2 DW 10 DUP (?)

String DB ‘123’

DSEG ENDS

(1)MOV AL, DATA1 10H->AL

(2)MOV BX, offset DATA2 DATA2代表的首地址赋给BX

(3)LEA SI, String String代表的首地址赋给BX

ADD DI,SI SI+DI->DI

9 假设数据项定义如下:

DATA1 DB ‘HELLO!GOOD MORNING’

DATA2 DB 20 DUP(?)

用串操作指令编写程序段,使其分别完成一下功能。

(1)从左到右将DATA1中的字符串传送到DATA2中;

LEA SI,DATA1

LEA DI,DATA2

MOV CX,20

CLD

REP MOVSB

(2)传送完后,比较DATA1和DATA2中的内容是否相同;

LEA SI,DATA1

LEA DI,DATA2

MOV CX,20

CLD

REPE CMPSB

(3)把DATA1中的第3个字节和第四个字节装入AX;

LEA SI,DATA1

ADD SI,2

LODSW

(4)将AX的内容存入DATA2+5开始的字节单元中;

LEA DI,DATA2

ADD DI,5

STOSW

10 执行下列指令后,AX寄存器中的内容是多少?

TABLE DW 10,20,30,40,50

ENTRY DW 3

……

MOV BX, Offset TABLE ADD BX, ENTRY

MOV AX,[BX] [AX]=1400H

11

微机原理习题4答案

微机原理习题4答案

DATA1 DATA2 DATA3 DB ‘HELLO!’ DATA4 EQU 12 DATA5 DD ABCDH

DATA ENDS

12 阅读下边的程序段,试说明它实现的功能是什么? DATA SEGMENT

DATA1 DB 'ABCDEFG' DATA ENDS CODE SEGMENT

ASSUME CS:CODE, DS:DATA AAAA: MOV AX,DATA MOV DS,AX

MOV BX, OFFSET DATA1 MOV CX,7 NEXT: MOV AH,2

MOV AL, [BX] XCHG AL,DL

INC BX INT 21H LOOP NEXT MOV AH,4CH INT 21H CODE ENDS END AAAA

此程序功能为:输出字符串‘ABCDEFG ’。

相关推荐
相关主题
热门推荐