博客
关于我
8086汇编语言:数据的输出(带符号输出、二进制输出、八进制输出、十六进制输出)
阅读量:391 次
发布时间:2019-03-05

本文共 3211 字,大约阅读时间需要 10 分钟。

一、题目说明

将一个数据存入AX中,分别以有符号数、二进制、八进制、十六进制的形式输出。


二、逐步求解

从本质上来讲,计算机不会关心你存储的是何种形式的数,只是将其视为01的数字串。因此,从本质上讲,各种形式的输出其实是这些01串的不同解读。

目录

① 有符号数形式的输出

② 二进制形式的输出
③ 八进制形式的输出
④ 十六进制形式的输出

① 有符号数形式的输出

当我们以有符号数的形式查看计算机中存储的01串时,最高位默认成为了符号位,0代表正数,1代表负数。

方案一:借助移位操作,将最高位换到最低位,再借助逻辑运算指令(如AND)将除最低位之外的所有位置为0,从而获取最高位的值,确定正负号输出。

方案二:借助cmp指令,查看符号位SF(SF=1为负数,SF=0为正数)。cmp ax,0可以不改变原数值,js用于判断SF位的结果。

:以下代码使用方案二。


② 二进制形式的输出

方案一:借助循环移位操作(ROL),将最高位移到最低位,再借助逻辑运算指令剥离最后一位值,循环上述操作直到最后一位。

方案二:借助除法操作(DIV),每次将AX除以2,压栈存余数,直到AX为零,输出压栈数据。

:以下代码使用方案一。


③ 八进制形式的输出

方案一:借助循环移位操作(ROL),每次将高三位移到低三位,再借助逻辑运算指令剥离低三位值,循环上述操作直到最后三位。

方案二:借助除法操作(DIV),每次将AX除以8,压栈存余数,直到AX为零,输出压栈数据。

:以下代码使用方案二。


④ 十六进制形式的输出

方案一:借助循环移位操作(ROL),每次将高四位移到低四位,再借助逻辑运算指令剥离低四位值,循环上述操作直到最后四位。

方案二:借助除法操作(DIV),每次将AX除以16,压栈存余数,直到AX为零,输出压栈数据。

:以下代码使用方案一。


三、问题解答

为什么上面输入负数使用除法就不行了呢?

因为在输入负数后,AX中是补码形式存储,高位是1。除法操作会导致AH和AL溢出,进而报错。因此建议使用移位操作。


四、最终汇总代码

以下是完整的汇总代码示例:

DATAS SEGMENT
CRLF DB 0AH,0DH,"$";回车换行
string DB "The original value was:", "$"
string1 DB "The binary form is:", "$"
string2 DB "The octal number system is:", "$"
string3 DB "The hexadecimal form is:", "$"
DATAS ENDS
STACKS SEGMENT
ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
lea dx, string
mov ah, 9
int 21h
mov ax, -123
call symoutput
lea dx, crlf
mov ah, 9
int 21h
lea dx, string1
mov ah, 9
int 21h
mov ax, -123
call binaryoutput
lea dx, crlf
mov ah, 9
int 21h
lea dx, string2
mov ah, 9
int 21h
mov ax, -123
call octaloutput
lea dx, crlf
mov ah, 9
int 21h
lea dx, string3
mov ah, 9
int 21h
mov ax, -123
call hexadecimaloutput
lea dx, crlf
mov ah, 9
int 21h
MOV AH, 4CH
INT 21H
;以下是各个输出函数的实现
symoutput proc
push bx
push dx
mov bx, ax
cmp ax, 0
js negative
jmp positive
negative:
mov dl, '-'
mov ah, 2
int 21h
neg bx
call output
jmp symoutputover
positive:
call output
symoutputover:
pop dx
pop bx
ret symoutput endp
;二进制输出函数
binaryoutput proc
push bx
push dx
mov dh, 0
binaryagain:
cmp dh, 16
je binaryover
rol ax, 1
mov bx, ax
and ax, 0001h
mov dl, al
add dl, 48
mov ah, 2
int 21h
inc dh
mov ax, bx
jmp binaryagain
binaryover:
pop dx
pop bx
ret binaryoutput endp
;八进制输出函数
octaloutput proc
push bx
push cx
push dx
rol ax, 1
mov bx, ax
and ax, 01h
mov dl, al
add dl, 48
mov ah, 2
int 21h
mov ax, bx
jmp octalagain
octalagain:
cmp dh, 5
je octalover
inc dh
mov cl, 3
rol ax, cl
mov bx, ax
and ax, 07h
mov dl, al
add dl, 48
mov ah, 2
int 21h
mov ax, bx
jmp octalagain
octalover:
pop dx
pop cx
pop bx
ret octaloutput endp
;十六进制输出函数
hexadecimaloutput proc
push bx
push cx
push dx
mov dh, 0
hexadecimalagain:
cmp dh, 4
je hexadecimalover
inc dh
mov cl, 4
rol ax, cl
mov cx, ax
and ax, 0fh
mov dl, al
cmp dl, 9
ja alphabet
add dl, 48
mov ah, 2
int 21h
mov ax, cx
jmp hexadecimalagain
alphabet:
add dl, 55
mov ah, 2
int 21h
mov ax, cx
jmp hexadecimalagain
hexadecimalover:
pop dx
pop cx
pop bx
ret hexadecimaloutput endp
;多位输出函数
output proc
push ax
push cx
push dx
mov ax, bx
mov cl, 10
mov ch, 0
divagain:
cmp ax, 0
je divover
inc ch
div cl
push ax
mov ah, 0
jmp divagain
divover:
cmp ch, 0
je outputover
pop ax
mov dl, ah
add dl, 48
mov ah, 2
int 21h
dec ch
jmp divover
outputover:
pop dx
pop cx
pop ax
ret output endp
CODES ENDS
END START

结论

通过以上代码,我们可以将数据以有符号数、二进制、八进制和十六进制的形式进行输出。每种形式的输出都通过不同的方法实现,确保了代码的高效性和可读性。

转载地址:http://hlmg.baihongyu.com/

你可能感兴趣的文章
Objective-C实现base85 编码算法(附完整源码)
查看>>
Objective-C实现basic graphs基本图算法(附完整源码)
查看>>
Objective-C实现BCC校验计算(附完整源码)
查看>>
Objective-C实现bead sort珠排序算法(附完整源码)
查看>>
Objective-C实现BeadSort珠排序算法(附完整源码)
查看>>
Objective-C实现bellman ford贝尔曼福特算法(附完整源码)
查看>>
Objective-C实现bellman-ford贝尔曼-福特算法(附完整源码)
查看>>
Objective-C实现bellman-ford贝尔曼-福特算法(附完整源码)
查看>>
Objective-C实现bellmanFord贝尔曼-福特算法(附完整源码)
查看>>
Objective-C实现BellmanFord贝尔曼-福特算法(附完整源码)
查看>>
Objective-C实现bezier curve贝塞尔曲线算法(附完整源码)
查看>>
Objective-C实现bfs 最短路径算法(附完整源码)
查看>>
Objective-C实现BF算法 (附完整源码)
查看>>
Objective-C实现Bilateral Filter双边滤波器算法(附完整源码)
查看>>
Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
查看>>
Objective-C实现binary search二分查找算法(附完整源码)
查看>>
Objective-C实现binary tree mirror二叉树镜像算法(附完整源码)
查看>>
Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
查看>>
Objective-C实现BinarySearchTreeNode树算法(附完整源码)
查看>>
Objective-C实现binarySearch二分查找算法(附完整源码)
查看>>