文章作者:冰血封情[EST]
信息来源:邪恶八进制 中国(
www.EvilOctal.com)
注意:本文是冰血封情对一个简单的ASM机试总结 没什么技术难度 很适合菜鸟看
刚刚考完IBMPC汇编语言程序设计 考试挺简单的 但是也得做个总结 因为中间被老师刁难了一下 不爽她
本次机试考了两道题目 就第一道题目来总结一下(因为她刁难我的就是这题目)
题目要求:
用IBMPC80x86汇编语言简单实现一个小程序 其作用是 让用户从键盘上输入一串字符 然后程序接受这些字符后统计里面非数字的字符个数 然后将结果显示出来
很简单的考试题目 是人都会 平时实验做过一个类似的 于是自然想偷懒了 人不偷懒天诛地灭呀 于是我一开始把程序写成这样(去过我blog的人都知道 这就是我以前做过分析的程序)
代码如下:
复制内容到剪贴板
代码:
;PROGRAM TITLE GOES HERE -- COUNT_CHAR
;*****************************************************************************
data segment ;define data segment
letter db 0 ;define letter A-->Z a-->z
digit db 0 ;define digit 0-->9
other db 0 ;define other char
data ends ;data segment end
;*****************************************************************************
code segment ;define code segment
;-----------------------------------------------------------------------------
main proc far ;begin of main part of program
assume cs:code,ds:data
start: ;start the execution address
mov letter,0
mov digit,0
mov other,0
input:
mov ah,01h ;input char
int 21h ;call cmd command
cmp al,0dh ;if enter
jz exit ;then exit
cmp al,'A' ;compare it to A
jb l1 ;if <A jump to l1
cmp al,'Z' ;compare it to Z
ja hello ;if >Z jump to hello
inc letter ;A----->Z
jmp input ;jump to input
l1:
cmp al,'0' ;compare it to 0
jb oth ;if <0 jump to oth
cmp al,'9' ;compare it to 9
ja oth ;if >9 <A jump to oth
inc digit ;0----->9
jmp input ;jump to input
hello:
cmp al,'a' ;compare it to a
jb oth ;if >Z <a jump to oth
cmp al,'z' ;compare it to z
ja oth ;if >z jump to oth
inc letter ;a----->z
jmp input ;jump to input
oth:
inc other ;other is char
jmp input ;jump to input
exit:
mov ah,4ch ;return to exit
int 21h ;call cmd command
ret ;return to DOS
main endp ;end of main part of program
;-----------------------------------------------------------------------------
code ends ;end of code segment
;*****************************************************************************
end start ;end assembly然后自然是臭美的让老师检查 其实这种程序很简单 没什么好臭美的 但谁让偶是一ASM初学者呢?
这个程序编译通过后(编译过程我就不说了 不是重点) 是用Debug运行的...如下:
引用:
Debug char.exe<回车呀>
-g<回车呀>
www1111@@@@@<回车呀>
-d0<回车呀>
这里debug会从开头开始查看程序 前三个数字分别是
03 04 05
就是按照程序中
复制内容到剪贴板
代码:
letter db 0 ;define letter A-->Z a-->z
digit db 0 ;define digit 0-->9
other db 0 ;define other char的顺序统计你输入的数字...
老师来后 我告诉她 03就是前面我输入的字母个数 而05是我前面输入的符号个数
他们加起来就是 非数字的个数了 3+5=8
但是老师非要我把8这个数字弄出来给她看 否则是不算通过:(
没办法...来吧 其实很简单 把程序修改一下就好了...
这里我来介绍一下原来程序的骨干流程:
其实原来的程序是这样的 首先我声明用
复制内容到剪贴板
代码:
letter db 0 ;define letter A-->Z a-->z
digit db 0 ;define digit 0-->9
other db 0 ;define other char分别用于存放字母 数字和符号 然后程序开始 把这三个数值先变成0 这不是多余的步骤 如果不这样 鬼知道计算机给它什么值 这样方便一点点加
复制内容到剪贴板
代码:
mov letter,0
mov digit,0
mov other,0这里是输入部分
复制内容到剪贴板
代码:
mov ah,01h ;input char
int 21h ;call cmd command首先判断是不是回车 如果第一个就是回车 那么退出
复制内容到剪贴板
代码:
cmp al,0dh ;if enter
jz exit ;then exit然后跟A比较 如果小于则送到l1 和Z比较大与则送到hello 剩下的全部送字符
其实我的程序注释很清晰的
复制内容到剪贴板
代码:
cmp al,'A' ;compare it to A
jb l1 ;if <A jump to l1
cmp al,'Z' ;compare it to Z
ja hello ;if >Z jump to hello
inc letter ;A----->Z大同小异 l1是判断是否是数字 如果不是就送oth
hello是看是不是小写字母 是就写入letter 不是也送oth
而oth 则把不是字母和数字的东东(当然是符号了)送到other 完成程序...
现在要修改的很简单 就是把不是数字的全送到另外一个地方就OK 程序修改如下:
(这年头 程序越来短 真不愧是偷懒的初学者)
复制内容到剪贴板
代码:
;PROGRAM TITLE GOES HERE -- COUNT_CHAR
;*****************************************************************************
data segment ;define data segment
digit db 0 ;define digit 0-->9
other db 0 ;define other char
data ends ;data segment end
;*****************************************************************************
code segment ;define code segment
;-----------------------------------------------------------------------------
main proc far ;begin of main part of program
assume cs:code,ds:data
start: ;start the execution address
mov digit,0
mov other,0
input:
mov ah,01h ;input char
int 21h ;call cmd command
cmp al,0dh ;if enter
jz exit ;then exit
cmp al,'0' ;compare it to 0
jb oth ;if <0 jump to oth
cmp al,'9' ;compare it to 9
ja oth ;if >9jump to oth
inc digit ;0----->9
jmp input ;jump to input
oth:
inc other ;other is char
jmp input ;jump to input
exit:
mov ah,4ch ;return to exit
int 21h ;call cmd command
ret ;return to DOS
main endp ;end of main part of program
;-----------------------------------------------------------------------------
code ends ;end of code segment
;*****************************************************************************
end start ;end assembly嘿嘿 经过测试 抓图如下

OK让老师来检查吧...当然Pass了...
本次考试的内容比较简单 但是ASM对于我来说还是一条很艰难的路 要想真的把它学扎实 学校的狗屁课程和书是根本不够的 更别指望靠那些老师:(
以后任何一个汇编程序 一定要弄到每行都明白是什么意思 否则 我决不罢休!
感谢各位听我唠叨:)