WEB开发网      濠电姷鏁告慨鐑藉极閸涘﹦绠鹃柍褜鍓氱换娑欐媴閸愬弶鎼愮痪鍓ф嚀閳规垿鎮╃€圭姴顥濋梺姹囧€楅崑鎾诲Φ閸曨垰绠涢柛顐f礃椤庡秹姊虹粙娆惧剳闁哥姵鍔欐俊鐢稿礋椤栨艾鍞ㄩ梺闈浤涙担鎻掍壕闁圭儤顨嗛埛鎺楁煕閺囥劌浜滄い蹇e弮閺屸€崇暆鐎n剛鏆犻柧浼欑到閵嗘帒顫濋悡搴d画缂佹鍨垮缁樻媴缁涘娈┑顔斤公缁犳捇銆佸鎰佹▌濠电姭鍋撳ù锝囩《閺€浠嬫煟濡鍤嬬€规悶鍎辫灃闁绘ê寮堕崯鐐电磼閸屾氨效鐎规洘绮忛ˇ瀵哥棯閹佸仮鐎殿喖鐖煎畷鐓庘槈濡警鐎崇紓鍌欑劍椤ㄥ棗鐣濋幖浣歌摕闁绘棃顥撻弳瀣煟濡も偓閻楀棗鈻撳Δ鍛拺閻犲洠鈧櫕鐏€闂佸搫鎳愭慨鎾偩閻ゎ垬浜归柟鐑樼箖閺呮繈姊洪棃娑氬婵☆偅鐟╅、娆掔疀閺冨倻鐦堥梺姹囧灲濞佳勭閿曞倹鐓曢柕濞垮劤閸╋絾顨ラ悙鏉戝妤犵偞锕㈤、娆撴嚃閳哄骞㈤梻鍌欐祰椤鐣峰Ο鑲╃煋妞ゆ棁锟ユ禍褰掓煙閻戞ɑ灏ù婊冪秺濮婅櫣绱掑Ο铏逛桓闂佹寧娲嶉弲娑滅亱闂佸憡娲﹂崹閬嶅煕閹达附鐓欓柤娴嬫櫅娴犳粌鈹戦垾鐐藉仮闁诡喗顨呴埥澶愬箳閹惧褰囩紓鍌欑贰閸犳牠顢栭崨鎼晣闁稿繒鍘х欢鐐翠繆椤栨粎甯涙繛鍛喘濮婄粯鎷呴悷閭﹀殝缂備浇顕ч崐鍨嚕缂佹ḿ绡€闁搞儯鍔嶅▍鍥⒑缁嬫寧婀扮紒瀣崌瀹曘垽鎮介崨濠勫幗闁瑰吋鐣崹濠氬煀閺囥垺鐓ユ慨妯垮煐閻撶喖鐓崶銉ュ姢缂佸宕电槐鎺旂磼濡偐鐣虹紓浣虹帛缁诲牆鐣峰鈧俊姝岊槺缂佽鲸绻堝缁樻媴缁涘娈愰梺鎼炲妺閸楀啿鐣烽鐐茬骇闁瑰濮靛▓楣冩⒑缂佹ɑ鈷掗柍宄扮墦瀵偊宕掗悙瀵稿幈闂佹娊鏁崑鎾绘煛閸涱喚鎳呮俊鍙夊姇铻i悶娑掑墲閺傗偓闂備胶绮崝鏇炍熸繝鍥у惞闁绘柨鐨濋弨鑺ャ亜閺冨洦顥夐柛鏂诲€濋幗鍫曟倷閻戞ḿ鍘遍梺鍝勬储閸斿本鏅堕鐐寸厱婵炲棗绻掔粻濠氭煛鐏炵晫效鐎规洦鍋婂畷鐔碱敆閳ь剙鈻嶉敐鍥╃=濞达絾褰冩禍鐐節閵忥絾纭炬い鎴濇川缁粯銈i崘鈺冨幍闁诲孩绋掑玻璺ㄧ不濮椻偓閺屻劌鈽夊Ο澶癸絾銇勯妸锝呭姦闁诡喗鐟╅、鏃堝礋椤撴繄绀勯梻鍌欐祰椤曟牠宕伴弽顐ょ濠电姴鍊婚弳锕傛煙椤栫偛浜版俊鑼额嚙閳规垿鍩勯崘銊хシ濡炪値鍘鹃崗妯侯嚕鐠囨祴妲堥柕蹇曞閳哄懏鐓忓璺虹墕閸旀挳鏌涢弬娆炬Ш缂佽鲸鎸婚幏鍛矙鎼存挸浜鹃柛婵勫劤閻挾鎲搁悧鍫濈瑨闁哄绶氶弻鐔煎礈瑜忕敮娑㈡煛閸涱喗鍊愰柡灞诲姂閹倝宕掑☉姗嗕紦 ---闂傚倸鍊搁崐鎼佸磹閻戣姤鍊块柨鏃堟暜閸嬫挾绮☉妯哄箻婵炲樊浜滈悡娑㈡煕濞戝崬骞樻い鏂挎濮婅櫣鎹勯妸銉︾彚闂佺懓鍤栭幏锟�
开发学院软件开发汇编语言 汇编源码系列之showmem 阅读

汇编源码系列之showmem

 2008-04-29 09:32:44 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹妞嬪孩顐芥慨姗嗗厳缂傛氨鎲稿鍫罕闂備礁婀遍搹搴ㄥ窗閺嶎偆涓嶆い鏍仦閻撱儵鏌i弴鐐测偓鍦偓姘炬嫹闂傚倸鍊搁崐鎼佸磹妞嬪海鐭嗗〒姘e亾妤犵偛顦甸弫鎾绘偐閹绘帞鈧參姊哄Ч鍥х仼闁诲繑鑹鹃悾鐑藉蓟閵夛妇鍘甸梺瑙勵問閸犳牠銆傛總鍛婄厱閹艰揪绱曟牎闂侀潧娲ょ€氫即鐛幒妤€绠f繝闈涘暙娴滈箖鏌i姀鈶跺湱澹曟繝姘厵闁绘劦鍓氶悘杈ㄤ繆閹绘帞澧涚紒缁樼洴瀹曞崬螖閸愬啠鍓濈换娑樼暆婵犱胶鏁栫紓浣介哺閹瑰洤鐣烽幒鎴僵闁瑰吀鐒﹂悗鎼佹⒒娴g儤鍤€闁搞倖鐗犻獮蹇涙晸閿燂拷濠电姷鏁告慨鐑藉极閸涘﹥鍙忔い鎾卞灩缁狀垶鏌涢幇闈涙灈鐎瑰憡绻冮妵鍕箻鐎靛摜鐣奸梺纭咁潐濞茬喎顫忕紒妯肩懝闁逞屽墮宀h儻顦查悡銈夋煏閸繃鍋繛宸簻鎯熼梺瀹犳〃閼冲爼宕濋敃鈧—鍐Χ閸℃鐟愰梺鐓庡暱閻栧ジ宕烘繝鍥у嵆闁靛骏绱曢崢顏堟⒑閹肩偛鍔楅柡鍛⊕缁傛帟顦寸紒杈ㄥ笚濞煎繘鍩℃担閿嬵潟闂備浇妗ㄩ悞锕傚箲閸ヮ剙鏋侀柟鍓х帛閺呮悂鏌ㄩ悤鍌涘闂傚倸鍊搁崐鎼佸磹妞嬪孩顐芥慨姗嗗厳缂傛氨鎲稿鍫罕闂備礁婀遍搹搴ㄥ窗閺嶎偆涓嶆い鏍仦閻撱儵鏌i弴鐐测偓鍦偓姘炬嫹  闂傚倸鍊搁崐鎼佸磹閻戣姤鍤勯柤鍝ユ暩娴犳氨绱撻崒娆掑厡缂侇噮鍨堕妴鍐川閺夋垹鍘洪悗骞垮劚椤︻垶宕¢幎鑺ョ厪闊洦娲栨牎闂佽瀵掗崜鐔奉潖閾忓湱纾兼俊顖氭惈椤秴顪冮妶鍡楀闁告鍥х叀濠㈣泛谩閻斿吋鐓ラ悗锝庡厴閸嬫挻绻濆顓涙嫼閻熸粎澧楃敮鎺撶娴煎瓨鐓曢柟鎯ь嚟閳藉鏌嶇紒妯荤叆妞ゎ偅绻堥幊婊呭枈濡顏归梻鍌欑閹诧紕绮欓幋锔哄亼闁哄鍨熼弸鏃堟煛閸愶絽浜剧紓浣虹帛缁嬫挻绂掗敃鍌氱<婵﹩鍓﹂悗鎶芥⒒娴e摜锛嶇紒顕呭灦楠炴垿宕堕鍌氱ウ闁诲函缍嗘禍鏍绩娴犲鐓欓梺顓ㄧ畱婢ь垱銇勯弬鍨偓瑙勭┍婵犲洦鍊锋い蹇撳閸嬫捇寮借濞兼牕鈹戦悩瀹犲闁稿被鍔庨幉姝岀疀濞戞ḿ鐤呴梺鍦檸閸犳寮查幖浣圭叆闁绘洖鍊圭€氾拷
核心提示:这个都是过去DOS时代的汇编源码,虽然已经过去了,汇编源码系列之showmem,但是对于学习汇编还是有帮助的,汇编语言只是程序员一门基础语言

这个都是过去DOS时代的汇编源码,虽然已经过去了,但是对于学习汇编还是有帮助的,汇编语言只是程序员一门基础语言,大多人掌握即可,不一定要深入研究.......

;************************************************************************
;*
;*   SHOWMEM (C) 1997 RonSoft.                    *
;*                                   *
;*   A little utility thats shows what's in a given mem position.  *
;*   Usage:                             *
;*   Showmem <segment,offset/startaddress>              *
;*   Where segment and offset are 16 bit hexadecimal values or    *
;*   a single 32 bit value.                     *
;*   Just like this:                         *
;*   Showmem 00000000 or Showmem 0.                 *
;*                                   *
;*   Ronald Nordberg.                        *
;*   Silverv刧en 3                          *
;*   907 50 Ume?                          *
;*   Sweden                             *
;*   christine.martinson@swipnet.se                 *
;*   http://home2.swipnet.se/~w-20064                *
;*                                   *
;*   THIS IS ALL PUBLIC DOMAIN FREEWARE.               *
;*   LET EM KNOW IF YOU USE MY CODE.                 *
;*                                   *
;************************************************************************
                    ;some euqates for readability
kbd       equ   16h       ;keyboard irq
msdos      equ   21h       ;MSDOS irq
reset      equ   0dh       ;disk reset
dfopen     equ   0fh       ;open disk file
dfclose     equ   10h       ;close disk file
searchf     equ   11h       ;search first
searchn     equ   12h       ;search next
seqread     equ   14h       ;sequential disk read
seqwrite    equ   15h       ;   "    " write
setdta     equ   1ah       ;set disk transfer area address
createf     equ   3ch       ;create file with handle
openf      equ   3dh       ;open file with handle
closef     equ   3eh       ;close file with handle
readf      equ   3fh       ;read from file with handle
writef     equ   40h       ;write to file with handle
setfp      equ   42h       ;set file pointer
allocmem    equ   48h       ;allocate memory
freemem     equ   49h       ;free memory
changebs    equ   4ah       ;change block size
findfirst    equ   4eh       ;find first file
exit      equ   4c00h      ;msdos exit
[BITS 16]                ;NASM stuff
[ORG 0x100]
s1:
    mov   ax,cs          ;get code segment
    mov   ds,ax          ;use it now
    mov   [comseg],ds       ;save it there    
    mov   si,0080h        ;DOS command line page 0
    lodsb              ;load size of command line
    cmp   al,0          ;anything on command line ?
    jbe   usage          ;noo, show usage
    cbw               ;extend AL to AX
    xchg  bx,ax          ;swap size to bx for indexing
    mov   byte [bx+si],0     ;null terminate command line
    call  parse          ;parse command line
    jmp   main          ;go on with main
usage: mov   bx,utext        ;pointer usage text
    jmp   errout         ;skip this
main:
    mov   si,inbuff        ;check for valid HEX input
    mov   bx,errt1        ;proper text
ishex: lodsb              ;get the char
    cmp   al,'0'
    jb   errout
    and   al,0dfh         ;force UPPERCASE
    cmp   al,'F'         ;>F ?
    ja   errout         ;yeahh, dump this
    loop  ishex
    call  hexbin         ;make hex bin
                    ;start address now in EDX
    mov   ax,dx          ;get low word (segment)
    mov   es,ax          ;start segment
    shr   edx,16         ;shift in offset
    mov   di,dx          ;start offset
dopage:
    push  es           ;save registers
    push  di
    push  ds
    push  si
    mov   ax,es
    mov   ds,ax          ;make ds=es
    mov   si,di          ;and si=di
    
    call  showpage        ;show it
    
    pop   si           ;restore registers
    pop   ds
    pop   di
    pop   es
    add   di,512         ;adjust memory position
    ;xor   ah,ah         ;wait for ANY key
    ;int   kbd
    
    mov   bx,text         ;show message
    call  write
    mov   ah,0          ;wanna see next screen ?
    int   kbd           ;chek out keyboard buffer
    and   al,0DFh         ;force UPPER CASE
    cmp   al,"Q"         ;wanna quit ?
    je   quit          ;yeahh
    jmp   dopage
errout:
    call  write
quit: 
    mov   ax,exit
    int   msdos
;***********************************************************
;*   Convert ascii hex to 32 bit binary
;*   Input = command line buffer, output EDX
;***********************************************************
hexbin:
    mov   si,inbuff        ;pointer command line buffer
    xor   edx,edx         ;clear binary output
aschexbin:
    lodsb
    cmp   al,'0'         ;< 0
    jb   notasc         ;yes invalid character
    cmp   al,'9'         ;<= 9
    jbe   astrip         ;yes, strip high 4 bits
    and   al,05fh         ;force upper case
    cmp   al,'A'         ;< ascii A
    jb   notasc         ;yes, invalid character
    cmp   al,'F'         ;> ascii F
    ja   notasc         ;yes, invalid character
    add   al,9          ;ok, add 9 for strip
astrip:
    and   al,0fh         ;strip high 4 bits
    mov   cx,4          ;set shift count
    shl   edx,cl         ;rotate EDX 4 bits
    xor   ah,ah          ;zero out AH
    cbw
    add   edx,eax         ;add digit to value
    jmp   aschexbin        ;continue
notasc: ret
;*********************************************************************
;*   Format and show the stuff in a "sector"
;*   Input SI
;*********************************************************************
showpage:
    mov   cx,32          ;32*16=512
arow:  push  cx
    mov   di,outline       ;output buffer
    mov   cx,16          ;process 16 bytes
hexrow: push  cx
    lodsb              ;load al with byte
    mov   dl,al          ;get value
    mov   cx,2          ;2 nibbles
chexb: push  cx           ;save that
    mov   cl,4          ;4 bits
    rol   dl,cl          ;rotate source left
    mov   al,dl          ;move digit into AL
    and   al,15          ;clear high nibble
    daa               ;adjust AL if A through F
    add   al,240         ;bump the carry
    adc   al,40h         ;convert HEX to ASCII
    stosb              ;copy to buffer
    pop   cx           ;get digit counter
    loop  chexb          ;next digit
    mov   al,32          ;copy a SPACE
    stosb
    pop   cx           ;restore loop counter
    loop  hexrow         ;loop on
    mov   al,32          ;copy 2 spaces
    stosb
    stosb
    sub   si,16          ;adjust source back
    mov   cx,16          ;copy ASCII bytes
cccp:  lodsb
    cmp   al,32          ;< SPACE ?
    jb   noa           ;yeahh, skip it
    stosb              ;no, store in buffer
    jmp   next
noa:  mov   al,'.'
    stosb
next  loop  cccp 
    mov   al,13
    stosb
    mov   al,10
    stosb
    mov   al,0          ;null terminate line
    stosb
    mov   bx,outline       ;show the line
    call  write
    pop   cx
    cmp   cx,17
    jne   nopause
    push  ds
    mov   ax,cs
    mov   ds,ax
    mov   bx,text1
    call  write
    pop   ds
    xor   ah,ah
    int   kbd
nopause:
    loop  arow          ;next 16 bytes
    ret
;************************************************************************'
;*   Convert bin WORD to HEX ascii. Input DX. Result in Numbuff   *
;************************************************************************
binhex: pusha   
    mov   di,numbuff       ;destination buffer
    mov   dx,[count]       ;binary number
    mov   cx,4          ;four nibbles
convhex:
    push  cx           ;save counter
    mov   cl, 4          ;4 bits
    rol   dx, cl         ;rotate source left
    mov   al, dl         ;move digit into AL
    and   al, 15         ;clear high nibble
    daa               ;adjust AL if A through F
    add   al, 240         ;bump the carry
    adc   al, 40h         ;convert HEX to ASCII
    stosb              ;copy to buffer
    pop   cx           ;get digit counter
    loop  convhex         ;next digit
    mov   al,32          ;copy a space
    stosb
    mov   al,0          ;null terminate
    stosb
    popa
    ret
;*************************************************************************
;*    Writes out the NULL terminated text supplied in BX.       *
;*    OR writes out data,BX and size,CX if called at lwrite.     *
;*************************************************************************
write: pusha
    mov   si,bx          ;copy to SI
    mov   cx,0          ;clear count
wloop: lodsb              ;load AL with SI
    cmp   al,0          ;end of line ?
    je   lwrite          ;yeahh
    inc   cx           ;no, incrase byte count
    jmp   wloop          ;test next byte
lwrite: mov   dx,bx          ;text address in DX
    mov   bx,1          ;filehandle standard output = 1
    mov   ah,writef        ;MS-DOS writefile with handle is 040
    int   msdos          ;write buffer to standard output
    popa
    ret               ;done
;*************************************************************************
;*   My kind of command line parsing. It just checks if there飐
;*   any blankspaces between the options. The parameters ends up
;*   in the inbuff separated by 0:s, binary zeroes.
;*************************************************************************
parse:
    mov   di,inbuff        ;our buffer
ifspc: cmp   byte [si],32      ;leading space ?
    jne   nospc          ;noo
    inc   si           ;yeahh, dump that
    jmp   ifspc          ;check next
nospc: mov   cx,1          ;were here, so we got one arg
copy1: lodsb              ;load byte SI to AL
    cmp   al,0          ;0 ?(end of line)
    je   done          ;yeahh
    cmp   al,32          ;SPACE ?
    je   cop2          ;yeah
    stosb              ;noo, move AL to DI, incrase DI
    jmp   copy1          ;go on
cop2:  mov   byte [di],0       ;null terminate
    add   cx,1
    inc   di           ;dump that byte(SPACE)
    jmp   copy1          ;back
done:  mov   byte [di],0       ;null terminate
    ret               ;return
;*************************** DATA STUFF **********************************
XMS_SEGMENT   dw 0
XMS_OFFSET   dw 0
inbuff     times 64 dw 0      ;128 byte command line buffer
outline     times 40 dw 0      ;buffer output line
numbuff     times 7 dw 0      ;word ascii number buffer
comseg     dw 0
count      dw 0
bcount     dw 0
acount     dw 0
;outbuff     times 512 db 0
utext      db   'Showmem (C) 1997 RonSoft.',13,10
        db   'Usage: Showmem [start address].',13,10
        db   'Start address = Hexadecimal.',13,10,0
text:      db   13,10,'Q = Quit. Any key = Next page.',13,10,0
text1:     db   13,10,'Any Key = Next 256 Bytes.',13,10,0
errt1:     db   'That address is not hexadecimal.',13,10,0
s2:
END

Tags:汇编 源码 系列

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接