欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

emu8086,汇编程序:屏幕输入字符串,设置寄存器的值

程序员文章站 2024-02-01 18:39:58
...

题目:
emu8086,汇编程序:屏幕输入字符串,设置寄存器的值
代码:

data segment
MaxSize db 20
ActualSize db 0
InputString db 20 DUP(0)
Temp1 dw 0
Temp2 dw 0
Temp3 dw 0
LoopCnt dw 0
Num dw 0
Msg1_NoEqualSign DB 0DH,0AH,'The expression does not have any equal sign!',0DH,0AH,'$' 
Msg2_NotVaildReg DB 0DH,0AH,'Not a vaild register!',0DH,0AH,'$'
ends

stack segment
ends

code segment
start:
;1.输入字符串
MOV AX,data
MOV DS,AX 
LEA DI,MaxSize
MOV AH,0AH
int 21h                            
;2.先把输入的值(=号后面的内容)转为数值放到AX里

;2-1.寻找等于号
MOV CX,0
MOV AL,'='            
MOV AH,0
FindTheEqualSign: 
LEA BX,InputString
ADD BX,CX
MOV BL,[BX]
CMP AX,BX
JZ TheEqualSignFound;找到等于号 
INC CX
CMP CL,ActualSize
JZ TheEqualSignNotFound;输入的字符中没有等于号
JMP FindTheEqualSign

;2-1->1找到,等于号的下标存入AX
TheEqualSignFound:
MOV AX,CX
JMP StringToInt         
;2-1->0没找到,程序结束
TheEqualSignNotFound:
MOV AH,9
LEA DI,Msg1_NoEqualSign
INT 21H
JMP End
   
;3.从等于号往后直到末尾的十六进制字符转化为实际数值
StringToInt:
MOV CH,0
MOV CL,ActualSize
SUB CX,AX
DEC CX
INC AX;AX先指向=号后面第一个字符
LEA BX,InputString
ADD BX,AX;求出=号后第一个字符的偏移地址
MOV DX,0
MOV AX,0
ParseNum:
MOV AH,0
MOV AL,[BX]
CMP AL,'A'
JNAE ZeroToNine;输入是字符0~9
JAE AtoF;输入字符是A~F
ZeroToNine:
SUB AL,30H
JMP E1
AtoF:
SUB AL,'A'
ADD AL,10
JMP E1
E1:
MOV Temp1,AX;T1暂存此时AX
MOV Temp2,CX;T2暂存此时CX
DEC CX
MOV AX,4
MUL CX
MOV Temp3,AX;T3存入要移位的次数
MOV AX,Temp1
MOV CX,Temp3
SAL AX,CL
MOV DX,Num
ADD DX,AX
MOV Num,DX
MOV CX,Temp2
INC BX
LOOP ParseNum 

;解析字符串的前两个字符是AX~DX中哪个
SelectReg:
LEA BX,InputString
MOV AL,[BX]
CMP AL,'A'
JZ IsA
CMP AL,'B'
JZ IsB
CMP AL,'C'
JZ IsC
CMP AL,'D'
JZ IsD
JMP NotVailedReg


    
IsA:
INC BX
MOV AL,[BX]
CMP AL,'L'
JZ IsAL
CMP AL,'H'
JZ IsAH
CMP AL,'X'
JZ IsAX
JMP NotVailedReg

    IsAL:
    LEA BX,Num
    MOV AL,[BX];取Num低位放入AL
    JMP End
    IsAH:
    LEA BX,Num
    MOV AH,[BX];取Num低位放入AH
    JMP End
    IsAX:
    MOV AX,Num
    JMP End

IsB:
INC BX
MOV BL,[BX]
CMP BL,'L'
JZ IsBL
CMP BL,'H'
JZ IsBH
CMP BL,'X'
JZ IsBX
JMP NotVailedReg

    IsBL:
    LEA BX,Num
    
    MOV BL,[BX];取Num低位放入AL
    JMP End
    IsBH:
    LEA BX,Num
    
    MOV BH,[BX];取Num低位放入AH
    JMP End
    IsBX:
    MOV BX,Num
    JMP End

IsC:
INC BX
MOV CL,[BX]
CMP CL,'L'
JZ IsCL
CMP CL,'H'
JZ IsCH
CMP CL,'X'
JZ IsCX
JMP NotVailedReg

    IsCL:
    LEA BX,Num
    
    MOV CL,[BX];取Num低位放入CL
    JMP End
    IsCH:
    LEA BX,Num
    
    MOV CH,[BX];取Num低位放入CH
    JMP End
    IsCX:
    MOV CX,Num
    JMP End   
    
IsD:
INC BX
MOV DL,[BX]
CMP DL,'L'
JZ IsDL
CMP DL,'H'
JZ IsDH
CMP DL,'X'
JZ IsDX
JMP NotVailedReg

    IsDL:
    LEA BX,Num
    
    MOV DL,[BX];取Num低位放入DL
    JMP End
    IsDH:
    LEA BX,Num
    
    MOV DH,[BX];取Num低位放入DH
    JMP End
    IsDX:
    MOV DX,Num
    JMP End

NotVailedReg:
MOV AH,9
LEA DI,Msg2_NotVaildReg
INT 21H
JMP End

End:
ends

end start ; set entry point and stop the assembler.