Post view

Assembly組合語言程式:stdio.asm、input.asm

; stdio.asm       MASM version 5.1

; Standard Input and Output Library

; Copyright(c) 1994 by Jackie Su.


        .286
        .model small

        .data
public  _numstr, _tonum, _ascii, _scan


        .data
_numstr db      80 dup(0)
_tonum  dw      0
_ascii  db      0
_scan   db      0


        .code
public  _clrscr, _gotoxy, _getvideomode, _getkey
public  _printstr, _printchar, _printchar, _printline, _prnnewline
public  _printint, _printhex, _printbin
public  _itoa, _htoa, _btoa
public  _atoi, _atoh
public  _inputstr

public  _fopen, _fclose, _fread, _fwrite, _fseek
public  _fcreate

        .code

_clrscr  proc
        pusha
        mov     al,0
        mov     bh,07H
        mov     cx,0
        mov     dh,24
        mov     dl,79
        mov     ah,06H
        int     10H
        mov     dx,0
        mov     bh,0
        mov     ah,02H
        int     10H
        popa
        ret
_clrscr  endp

_gotoxy proc
        push ax
        push bx
        dec  dl
        dec  dh
        mov  bh,0
        int  10H
        pop  bx
        pop  ax
        ret
_gotoxy endp

_getvideomode proc
        push bx
        mov  ah,0fH
        int  10H
        pop  bx
        ret
_getvideomode endp

_getkey proc
        push  ax
        mov   ah,0
        int   16H
        mov   _ascii,al
        mov   _scan,ah
        pop   ax
        ret
_getkey endp

_printstr proc
        push    ax
        mov     ah,09H
        int     21H
        pop     ax
        ret
_printstr endp

_printchar proc
        push    ax
        mov     ah,02H
        int     21H
        pop     ax
        ret
_printchar endp

_printline proc
        push    ax
        mov     ah,09H
        int     21H
        call    _prnnewline
        pop     ax
        ret
_printline endp

_prnnewline proc
        push    dx
        mov     dl,13
        call    _printchar
        mov     dl,10
        call    _printchar
        pop     dx
        ret
_prnnewline endp

_itoa    proc
        pusha
        mov     cx,0
        mov     bx,10
@@divide:
        mov     dx,0
        div     bx
        push    dx
        inc     cx
        cmp     ax,0
        jnz     @@divide
        lea     bx,_numstr
@@output:
        pop     ax
        add     al,48
        mov     [bx],al
        inc     bx
        loop    @@output
        mov     byte ptr [bx],'$'
        popa
        ret
_itoa    endp

_htoa    proc
        pusha
        mov     cx,4
@@convert:
        mov     dx,ax
        and     dx,000fH
        push    dx
        shr     ax,4
        loop    @@convert
        mov     cx,4
        lea     bx,_numstr
@@output2:
        pop     ax
        cmp     al,10
        jae     @@hex2
        add     al,48
        jmp     @@out2
@@hex2:
        add     al,55
@@out2:
        mov     [bx],al
        inc     bx
        loop    @@output2
        mov     byte ptr [bx],'$'
        popa
        ret
_htoa    endp

_btoa    proc
        pusha
        mov     cx,16
@@convert3:
        mov     dx,ax
        and     dx,0001H
        push    dx
        shr     ax,1
        loop    @@convert3
        mov     cx,16
        lea     bx,_numstr
@@output3:
        pop     ax
        add     al,48
        mov     [bx],al
        inc     bx
        loop    @@output3
        mov     byte ptr [bx],'$'
        popa
        ret
_btoa    endp

_atoi    proc
        pusha
        mov     bx,dx
        mov     di,0
        mov     si,0
        mov     ax,0
        mov     _tonum,0
        cmp     byte ptr [bx],'-'
        jne     @@getchar1
        mov     di,1
        inc     bx
@@getchar1:
        mov     al,[bx]
        cmp     al,'$'
        je      @@getok1
        sub     al,'0'
        push    ax
        inc     bx
        inc     si
        jmp     @@getchar1
@@getok1:
        cmp     si,0
        jz      @@exit1
        mov     cx,1
        mov     bx,10
@@multipli:
        pop     ax
        mul     cx
        add     _tonum,ax
        mov     ax,cx
        mul     bx
        mov     cx,ax
        dec     si
        jnz     @@multipli
        cmp     di,0
        jz      @@exit1
        neg     _tonum
@@exit1:
        popa
        ret
_atoi    endp

_atoh    proc
        pusha
        mov     bx,dx
        mov     si,0
        mov     ax,0
        mov     _tonum,0
@@skip0:
        cmp     byte ptr [bx],'0'
        jne     @@getchar2
        inc     bx
        jmp     @@skip0
@@getchar2:
        mov     al,[bx]
        cmp     al,'$'
        je      @@getok2
        cmp     al,'A'
        jae     @@hex0
        sub     al,48
        jmp     @@hexok0
@@hex0:
        cmp     al,'a'
        jae     @@hex1
        sub     al,55
        jmp     @@hexok0
@@hex1:
        sub     al,87
@@hexok0:
        push    ax
        inc     bx
        inc     si
        jmp     @@getchar2
@@getok2:
        cmp     si,0
        jz      @@exit2
        mov     cx,1
        mov     bx,16
@@multipli1:
        pop     ax
        mul     cx
        add     _tonum,ax
        mov     ax,cx
        mul     bx
        mov     cx,ax
        dec     si
        jnz     @@multipli1
@@exit2:
        popa
        ret
_atoh    endp

_printint proc
        push    dx
        call    _itoa
        lea     dx,_numstr
        call    _printstr
        pop     dx
        ret
_printint endp

_printhex proc
        push    dx
        call    _htoa
        lea     dx,_numstr
        call    _printstr
        pop     dx
        ret
_printhex endp

_printbin proc
        push    dx
        call    _btoa
        lea     dx,_numstr
        call    _printstr
        pop     dx
        ret
_printbin endp

_inputstr proc
        pusha
        mov     ah,0aH
        int     21H
        mov     bx,dx
        mov     dx,0
        inc     bx
        mov     dl,[bx]
        add     bx,dx
        inc     bx
        mov     byte ptr[bx],'$'
        popa
        ret
_inputstr endp

_fopen  proc
        mov     ah,03dH
        int     21H
        ret
_fopen  endp

_fcreate proc
        mov     cx,0
        mov     ah,03cH
        int     21H
        ret
_fcreate endp

_fclose proc
        push    ax
        mov     ah,03eH
        int     21H
        pop     ax
        ret
_fclose endp

_fread  proc
        mov     ah,03fH
        int     21H
        ret
_fread  endp

_fwrite proc
        mov     ah,040H
        int     21H
        ret
_fwrite endp

_fseek  proc
        mov     ah,042H
        int     21H
        ret
_fseek  endp

        end


; input.asm

        .286
        .model  small
        .stack
        .data
demo    db      'INPUT program Version 1.0 Copyright(c) 1994 by Brent Su.$'
prompt  db      'input your name : $'
hello   db      'Hello ! $'
strlen  db      0
strsize db      0
string  db      256 dup(0)

; Assembly Function
        .code
clrscr  proc
        pusha
        mov     al,0
        mov     bh,07H
        mov     cx,0
        mov     dh,24
        mov     dl,79
        mov     ah,06H
        int     10H
        mov     dx,0
        mov     bh,0
        mov     ah,02H
        int     10H
        popa
        ret
clrscr  endp

printstr proc
        push    ax
        mov     ah,09H
        int     21H
        pop     ax
        ret
printstr endp

printchar proc
        push    ax
        mov     ah,02H
        int     21H
        pop     ax
        ret
printchar endp

printline proc
        pusha
        mov     ah,09H
        int     21H
        mov     dl,10
        call    printchar
        mov     dl,13
        call    printchar
        popa
        ret
printline endp

prnnewline proc
        push    dx
        mov     dl,10
        call    printchar
        mov     dl,13
        call    printchar
        pop     dx
        ret
prnnewline endp

inputstr proc
        pusha
        mov     ah,0aH
        int     21H
        mov     bx,dx
        mov     dx,0
        inc     bx
        mov     dl,[bx]
        add     bx,dx
        inc     bx
        mov     byte ptr[bx],'$'
        popa
        ret
inputstr endp


; main program
        .code
start:
        mov     ax,@data
        mov     ds,ax

        call    clrscr

        lea     dx,demo
        call    printline

        lea     dx,prompt
        call    printstr

        mov     strlen,20
        lea     dx,strlen
        call    inputstr
        call    prnnewline

        lea     dx,hello
        call    printstr
        lea     dx,string
        call    printline

        mov     ax,4c00H
        int     21H

        end     start

蘇言霖 2013/09/10 0 1588
Comments
Order by: 
Per page:
 
  • There are no comments yet
Rate
0 votes
Post info
蘇言霖
「超級懶貓級」社群網站站長
2013/09/10 (3853 days ago)
Actions