当前位置: 代码迷 >> 汇编语言 >> 怎么输入一个十六进制数
  详细解决方案

怎么输入一个十六进制数

热度:3481   发布时间:2013-02-26 00:00:00.0
如何输入一个十六进制数?
目前只知道用0AH号功能将其当做字符串输入,但是要对其进行运算就很麻烦,如何直接输入一个十六进制数(不超过四位)?

------解决方案--------------------------------------------------------
无论你输入的是什么,cpu都把他转换成二进制存在内存中。我这里有一个早以前我的一个练习。你烟酒一下

Assembly code
assume    cs:code,ds:data,ss:stackdata    segmentmsg1    db    'Choice digit type:[1]Hex [2]Dec [3]Oct [4]Bin:$'msg2    db    'Input switch type:[1]Hex [2]Dec [3]Oct [4]Bin:$'msg3    db    'Input :$'msg4    db    'Result:$'var1    db    0var2    db    0buff    db    16 dup (0)error    db    'Invalid number or Out of compass !$'tab1    dw    10000,1000,100,10,1tab2    dw    4096,512,64,8,1tab3    dw    8000h,4000h,2000h,1000h,800h,400h,200h,100h,80h,        40h,20h,10h,8h,4h,2h,1hsum    dw    0data    endsstack    segment stack    dw 128 dup (0)stack    endscode    segmentstart:    mov ax,data    mov ds,ax;------------------------------------------------;    mov dx,offset msg1    mov ah,9    int 21h    mov ah,1    int 21h    cmp al,0dh    jz _err    cmp al,'1'    jb _err    cmp al,'4'    ja _err    mov var1,al        call crlf;------------------------------------------------;    mov dx,offset msg2    mov ah,9    int 21h    mov ah,1    int 21h    cmp al,0dh    jz _err    cmp al,'1'    jb _err    cmp al,'4'    ja _err        mov var2,al    call crlf;------------------------------------------------;    mov dx,offset msg3    mov ah,9    int 21h;    cmp var1,31h    jz inhex    cmp var1,32h    jz indec    cmp var1,33h    jz inoct    cmp var1,34h    jz inbin    jmp _err;------------------------------------------------;;;check hex-input;inhex:        mov cx,4    mov di,offset buff_inhex:    mov ah,1    int 21h    cmp al,0dh    jz none    cmp al,'0'    jb _err    cmp al,'9'    ja d;    sub al,30h    jmp jx1d:        cmp al,'A'    jb _err    cmp al,'F'    ja x;    sub al,37h    jmp jx1x:        cmp al,'a'    jb _err    cmp al,'f'    ja _err;    sub al,57hjx1:        mov [di],al    inc di    dec cx    jcxz ol1    jmp _inhexol1:    jmp a2h;------------------------------------------------;;;check dec-input;indec:        mov si,0    mov cx,5    mov di,offset buff_indec:    mov ah,1    int 21h    cmp al,0dh    jz ol2    cmp al,'0'    jb _err    cmp al,'9'    ja _err    sub al,30h    mov [di],al    inc di    inc si    dec cx    jcxz ol2    jmp _indecol2:    jmp d2h;------------------------------------------------;;check oct-input;inoct:        mov si,0    mov cx,5    mov di,offset buff_inoct:    mov ah,1    int 21h    cmp al,0dh    jz ol3    cmp al,'0'    jb _err    cmp al,'8'    ja _err    sub al,30h    mov [di],al    inc di    inc si    dec cx    jcxz ol3    jmp _inoctol3:    jmp o2h;------------------------------------------------;;check bin-input;inbin:        mov si,0    mov cx,16    mov di,offset buff_inbin:    mov ah,1    int 21h    cmp al,0dh    jz ol4    cmp al,'0'    jb _err    cmp al,'1'    ja _err    sub al,30h    mov [di],al    inc di    inc si    dec cx    jcxz ol4    jmp _inbinol4:    jmp b2h    ;------------------------------------------------;a2h:    mov cx,12    mov si,offset buff    xor dx,dxload:        mov al,[si]    cmp al,0    jz ok1    inc si    cmp al,40h    jb sz    cmp al,47h    jb zd    cmp al,67h    jb zxsz:        sub al,30h    mov ah,0    shl ax,cl    add dx,ax    sub cx,4    jmp a2hjxzd:    sub al,37h    mov ah,0    shl ax,cl    add dx,ax    sub cx,4    jmp a2hjxzx:        sub al,57h    mov ah,0    shl ax,cl    add dx,ax    sub cx,4a2hjx:        cmp cx,0fffch    jz ok1    jmp loadok1:    push dx    jmp show;------------------------------------------------;d2h:            mov bx,offset buff    add bx,si    dec bx    mov di,offset tab1    add di,8    mov cx,si    shijia:        mov al,[bx]    xor ah,ah    mul word ptr [di]    add sum,ax    jc _err    sub di,2    dec bx    loop shijia    push word ptr sum    jmp show;------------------------------------------------;o2h:    mov bx,offset buff    add bx,si    dec bx    mov di,offset tab2    add di,8    mov cx,sibajia:        mov al,[bx]    xor ah,ah    mul word ptr [di]    add sum,ax    jc _err    sub di,2    dec bx    loop bajia    push word ptr sum    jmp show;------------------------------------------------;b2h:    mov bx,offset buff    add bx,si    dec bx    mov di,offset tab3    add di,30    mov cx,sierjia:    mov al,[bx]    xor ah,ah    mul word ptr [di]    add sum,ax    jc _err    sub di,2    dec bx    loop erjia    push word ptr sum    jmp show;------------------------------------------------;show:    call crlf    mov dx,offset msg4    mov ah,9    int 21h    cmp var2,31h    jz _hex    cmp var2,32h    jz _dec    cmp var2,33h    jz _oct    cmp var2,34h    jz _bin_hex:        pop dx    call disphex    jmp over_dec:        pop dx    call dispdec    jmp over_oct:        pop dx    call dispoct    jmp over_bin:    pop dx    call dispbin    jmp over    ;------------------------------------------------;none:        cmp si,0    jz over_err:    call crlf    mov dx,offset error    mov ah,9    int 21hover:    mov ah,4ch    int 21h;crlf    proc    mov ah,2    mov dl,0dh    int 21h    mov dl,0ah    int 21h    retcrlf    endp;err    proc    mov dx,offset error    mov ah,9    int 21h    reterr    endp;disp    proc    mov dx,offset msg4    mov ah,9    int 21h    retdisp    endp;;input:dxdisphex    proc uses dx    mov cx,4d1:    push cx    mov cl,4    rol dx,cl    push dx    and dx,0fh    cmp dl,10    jb d10    add dl,37h    mov ah,2    int 21h    jmp jx2d10:    add dl,30h    mov ah,2    int 21hjx2:    pop dx    pop cx    loop d1    mov dl,'H'    mov ah,2    int 21h    call crlf    retdisphex    endp;;input:dxdispdec    proc uses dx    mov ax,dx    xor dx,dx    mov bx,10    mov cx,0d2:    cmp ax,10    jb ok2    div bx    add dl,30h    push dx    xor dx,dx    inc cx    jmp d2ok2:    add al,30h    push ax    inc cx<F12>d3:    pop dx    mov ah,2    int 21h    loop d3    mov dl,'D'    mov ah,2    int 21h    call crlf    retdispdec    endp;;input:dxdispoct    proc uses dx    mov bx,8    mov cx,0    mov ax,dx    xor dx,dxd3:    cmp ax,8    jb ok3    div bx    add dl,30h    push dx    xor dx,dx    inc cx    jmp d3ok3:    add al,30h    push ax    inc cxd4:    pop dx    mov ah,2    int 21h    loop d4    mov dl,'Q'    mov ah,2    int 21h    call crlf    retdispoct    endp;;input:dxdispbin    proc     mov cx,16    mov ax,dx    xor dx,dxd5:    rol ax,1    adc dl,0    push ax    add dl,30h    mov ah,2    int 21h    xor dx,dx    pop ax    loop d5    mov dl,'B'    mov ah,2    int 21h    retdispbin    endp;code    endsend    start
  相关解决方案