Skip to content

Instantly share code, notes, and snippets.

@fffonion
Last active December 28, 2015 16:09
Show Gist options
  • Save fffonion/7527358 to your computer and use it in GitHub Desktop.
Save fffonion/7527358 to your computer and use it in GitHub Desktop.
HUST微机原理实验x3 感受一下寄存器脑残粉的恶意
data segment
OBUF DB '$'
OBUF1 DB 0AH,0DH,'Please input N(0-255):$'
IBUF DB 3 DUP(?),'=$'
OUT_D DB 0AH,0DH,'N=$'
OUT_H DB 'H=$'
OUT_B DB 'B',0AH,0DH,'$'
data ends
code segment
begin proc far
assume cs:code,ds:data
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax
LEA DX,OBUF1
MOV AH,9
INT 21H
;*****************************DEC2HEX*************************************
;
MOV CH, 3 ;最长3位
INPUT:
MOV AH,01H ;输入0~9,a~f,A~F的数
INT 21H
;合法性判断
CMP AL,0DH ;回车
JZ INPUT_DONE
MOV BX, 3
SUB BL, CH
MOV IBUF[BX], AL
CMP AL,'0'
JB INPUT
CMP AL,'9'
JA INPUT
DEC CH
JNZ INPUT
INPUT_DONE:
MOV BX, 0
MOV BL, 3
SUB BL, CH ;输入位数-1
MOV DL, BL
D2H:
MOV AX, 0
SUB BL, DL ;计算偏移量
MOV AL, IBUF[BX]
ADD BL, DL
SUB AL, 30H ;转真实数字
ADD AL, DH ;之前的结果
DEC DL
JZ EXIT_D2H
MOV CX, 0A00H
MUL CH ;x10
MOV DH, AL ;相加
JMP D2H
EXIT_D2H:
MOV BL, AL ;存入BL
LEA DX, OUT_D ;printf('\nN=')
MOV AH,9
INT 21H
LEA DX, IBUF ;printf('123=')
INT 21H
MOV AL, BL
AND AL, 0FH
CMP AL, 0AH
JB ABCDEF
ADD AL, 7H
ABCDEF:
ADD AL, 30H
MOV AH, BL
AND AH, 0F0H
MOV CL, 4
SHR AH, CL
CMP AH, 0AH
JB ABCDEF2
ADD AH, 7H
ABCDEF2:
ADD AH, 30H
MOV CX, AX
MOV AH, 02H
CMP CH,30H ;若十位为空
JE GEWEI
MOV DL,CH ;输出十位
INT 21H
GEWEI:
MOV DL,CL ;输出个位
INT 21H
LEA DX,OUT_H
MOV AH,9
INT 21H ;printf('H=')
;*****************************HEX2BIN*************************************
;
MOV BH, BL
MOV BL, 0
MOV CX, 8
DISP_BIN:
MOV DL, 0
ROL BH, 1 ;SHIFT TO CF
RCL DL, 1 ;GET INT FROM CF
ADD DL, 30H ;转ASCII
MOV AH, 2
INT 21H
LOOP DISP_BIN
LEA DX,OUT_B
MOV AH,9
INT 21H
ret
begin endp
code ends
end begin
data segment
OBUF DB '$'
OBUF1 DB 0AH,0DH,'Please input N(00-FF):$'
OUT_H DB 0AH,0DH,'N=',2 DUP(?),'H=$'
OUT_B DB 'B=$'
OUT_D DB 0AH,0DH,'$'
data ends
code segment
begin proc far
assume cs:code,ds:data
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax
LEA DX,OBUF1
MOV AH,9
INT 21H
;*****************************HEX2BIN*************************************
;
MOV CH,2 ;2位的十六进制数
MOV CL,4 ;移位次数
MOV BX,0 ;存放二进制
INPUT:
SHL BX, CL ;左移4位(一个hex占4位bin)
MOV AH,01H ;输入0~9,a~f,A~F的数
INT 21H
;合法性判断
CMP AL,'0'
JB INPUT
CMP AL,'f'
JA INPUT
MOV DX,BX ;备份
MOV BX, 6
SUB BL, CH
MOV OUT_H[BX], AL ;拼接
MOV BX,DX ;还原
CMP AL,'9'
JBE NUM ;输入的数为0~9
CMP AL,'@'
JBE INPUT
CMP AL,'F'
JBE NON_NUMERIC ;输入的数为A~F
CMP AL,'a'
JB INPUT
CMP AL,'f'
JBE UPPER ;输入的数为a~f
NUM:
AND AL,0FH ;0-9
JMP BINARY
NON_NUMERIC: ;a-f
AND AL,0FH
ADD AL, 9
JMP BINARY
UPPER:
SUB AL,20H ;小写转大写 在与操作
AND AL,0FH ;转换为:1010B~1111B
ADD AL, 9
JMP BINARY
BINARY:
OR BL, AL ;拼接
DEC CH
JNZ INPUT ;next
LEA DX,OUT_H
MOV AH,9
INT 21H ;printf('N=9BH=')
MOV CX, 8 ;将8位二进制数一位位地转换成ASCII码显示
MOV BH,BL ;备份
DISP_BIN:
MOV DL, 0
ROL BH, 1 ;SHIFT TO CF
RCL DL, 1 ;GET INT FROM CF
ADD DL, 30H ;转ASCII
MOV AH, 2
INT 21H
LOOP DISP_BIN
LEA DX,OUT_B
MOV AH,9
INT 21H ;printf('B=')
;此时BH中存了二位16进制
;*****************************HEX2DEC*************************************
MOV AX, 0
MOV DX, 0
MOV CX, 0
;MOV DL, BH ;备份
MOV AL, BH
MOV BX, 64H
DIV BX ;/100
MOV BH, DL ;保存余数
CMP AL, 0
JZ NEXT_PRT
MOV CL, 0FFH ;记录 百位不为0
MOV DL, AL ;商不为0则打印商
ADD DL, 30H
MOV AH, 2
INT 21H
NEXT_PRT:
MOV AX, 0
MOV DX, 0
MOV AL, BH
MOV BX, 0AH
DIV BX ;/10
MOV BH, DL ;保存余数
CMP AL, CL
JZ LAST_PRT ;百位是0十位也是0则不打印
MOV DL, AL
ADD DL, 30H
MOV AH, 2
INT 21H
LAST_PRT:
MOV DL, BH ;打印余数
ADD DL, 30H
MOV AH, 2
INT 21H
LEA DX,OUT_D
MOV AH,9
INT 21H ;printf('\n')
ret
begin endp
code ends
end begin
stck segment stack 'stack'
dw 32 DUP(?)
stck ends
data segment
OBUF1 DB 0AH,0DH,'Please input n(1-9):$'
OBUF2 DB 0AH,0DH,'The result is:$'
data ends
code segment
begin proc far
assume ss:stck,cs:code,ds:data
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax
MOV DX,OFFSET OBUF1
MOV AH,9
INT 21H
INPUT: ;MOV DX,OFFSET AX
MOV AH,01H
INT 21H ;存放到AL中。
SUB AL,30H
JL EXIT ;<asc(0)
MUL AL ;计算平方。
MOV BX,0AH
DIV BX
ADD AH,DL
XCHG AL,AH
ADD AX,3030H ;转换成对应的ASCII
MOV BX,AX
OUTPUT:MOV DX,OFFSET OBUF2
MOV AH,9
INT 21H
MOV AH, 02H
CMP BH,30H ;若十位为空
JE GEWEI
MOV DL,BH ;输出十位
INT 21H
GEWEI: MOV DL,BL ;输出个位
INT 21H
EXIT: MOV AH,4CH
INT 21H
ret
begin endp
code ends
end begin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment