汇编编程实例.doc
汇编编程实例.txt你妈生你的时候是不是把人给扔了把胎盘养大?别把虾米不当海鲜。别把虾米不当海鲜。案例1判断闰年程序data segment ;定义数据段 infon db 0dh,0ah,'Please input a year: $' Y db 0dh,0ah,'This is a leap year! $' N db 0dh,0ah,'This is not a leap year! $' w dw 0 buf db 8 db ? db 8 dup(?)data endsstack segment stack db 200 dup(0)stack endscode segment assume ds:data,ss:stack,cs:code start:mov ax,data mov ds,ax lea dx,infon ;在屏幕上显示提示信息 mov ah,9 int 21h lea dx,buf ;从键盘输入年份字符串 mov ah,10 int 21h mov cl, buf+1 lea di,buf+2 call datacate call ifyears jc a1 lea dx,n mov ah,9 int 21h jmp exit a1: lea dx,y mov ah,9 int 21h exit: mov ah,4ch int 21h datacate proc near; push cx; dec cx lea si,buf+2 tt1: inc si loop tt1 ;lea si,cxdi pop cx mov dh,30h mov bl,10 mov ax,1 l1: push ax sub byte ptr si,dh mul byte ptr si add w,ax pop ax mul bl dec si loop l1 ret datacate endp ifyears proc near push bx push cx push dx mov ax,w mov cx,ax mov dx,0 mov bx,4 div bx cmp dx,0 jnz lab1 mov ax,cx mov bx,100 div bx cmp dx,0 jnz lab2 mov ax,cx mov bx,400 div bx cmp dx,0 jz lab2 lab1: clc jmp lab3 lab2: stc lab3: pop dx pop cx pop bx ret ifyears endpcode ends end start案例2产生随机数并运算的程序DATA SEGMENT CC DB '+' ww db '-' GG DB '=' II DB 0DH,0AH,'$' AA DB 5 DUP(?) www dw 0 ghh db '0',0dh,0ah,'$' bnm dw 0DATA ENDSSTACK SEGMENT DB 200 DUP(0)STACK ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA,SS:STACKSTART: MOV AX,DATA MOV DS,AX MOV CX,20 CALL RAND ;产生一随机数 CALL MCAT ;将随机数转换为16进制表示的ASCII码字符 MOV AX,BX PUSH AX CMP www,0 ;WWW=0? JE A1 ;执行'+' CMP www,1 ;www=1? JE A2 ;执行'-' A1: MOV DL,CC ;显示'+' MOV AH,2 INT 21H CALL RAND ;产生另一随机数 add bx,0fh CALL MCAT ;将随机数转换为16进制表示的ASCII码字符 MOV DL,GG ;显示'=' MOV AH,2 INT 21H POP AX ADD AX,BX cmp ax,0 jne yjw1 lea dx,ghh mov ah,9 int 21h jmp qq1 yjw1: JMP B1 A2: MOV DL,WW ;显示'-' MOV AH,2 INT 21H mov cx,65535 mai1: loop mai1 CALL RAND1 CALL MCAT MOV DL,GG MOV AH,2 INT 21H POP AX Sbb AX,BX cmp ax,0 jne yjw2 lea dx,ghh mov ah,9 int 21h jmp qq1 yjw2: JMP B1 B1: MOV BX,AX CALL MCAT lea DX,II MOV AH,9 INT 21H QQ1: MOV AH,4CH INT 21HMCAT PROC ;将随机数转换为16进制表示的ASCII码字符子程序 PUSH AX push bx PUSH CX PUSH DX CMP BX,9 JA S1 PUSH AX PUSH BX PUSH CX PUSH DX mov ax,bx mov bl,5 div bl cmp ah,3 jae vb1 mov www,1 jmp vn1 vb1:mov www,0 vn1:pop dx pop cx pop bx pop ax ADD BL,30H MOV AA,BL MOV AA+1,'$' LEA DX,AA MOV AH,9 INT 21H JMP s3 s1:MOV CL,4 MOV AL,0 PUSH BX SHL BX,CL CMP BH,9 JBE V1 SUB BH,9 ADD BH,40H JMP MM1 V1:ADD BH,30H MM1:MOV AA,BH POP BX AND BL,0FH PUSH AX PUSH BX PUSH CX PUSH DX mov ax,bx mov bl,5 div bl cmp ah,3 jae vb2 mov www,1 jmp vn2 vb2:mov www,0 vn2:pop dx pop cx pop bx pop ax CMP BL,9 JBE TT1 SUB BL,9 ADD BL,40H JMP RR1TT1: ADD BL,30HRR1: MOV AA+1,BL MOV AA+2,'$' LEA DX,AA MOV AH,9 INT 21H s3:POP DX POP CX POP BX POP AX RETMCAT ENDPRAND PROC PUSH CX PUSH DX PUSH AX STI MOV AH,0 ;读时钟计数器值 INT 1AH MOV AX,DX ;清高6位 AND AH,3 MOV DL,101 ;除101,产生0100余数 DIV DL MOV BL,AH ;余数存BX,作随机数 POP AX POP DX POP CX RETRAND ENDPRAND1 PROC PUSH CX PUSH DX PUSH AX STI MOV AH,0 INT 1AH MOV ax,cx AND AH,3 MOV DL,101 DIV DL MOV BL,AH POP AX POP DX POP CX RETRAND1 ENDPCODE ENDS END START 案例3计算平台长度程序data segmentobf db '* welcome you !*',0dh,0ah db ' input data:','$'a0 db ?a1 db ?b0 db ?b1 db ?a db ?b db ?data endsstack segment stack db 200 dup(0) stack endscode segment assume cs:code, ds:data,ss:stackmain:mov ax, data mov ds, ax mov ah, 9 lea dx, obf int 21h inc dx mov dl, 13 mov ah, 2 int 21h mov dl,10 int 21h mov ah,1 int 21h cmp al, 13 jz lab1 mov a0, al mov a, al mov a1, 1x1: mov ah, 1 int 21h cmp al, 13 jz lab1 mov b, al mov bl, b cmp bl, a jnz x2 add a1, 1 jmp x1x2: mov b0, bl mov b1, 1x3: mov ah, 1 int 21h mov a, al cmp bl,a jnz x4 add b1,1 jmp x3x4: mov cl, b1 cmp cl, a1 jb y1 mov cl, b0 mov a0, cl mov cl, b1 mov a1,cl y1: cmp al, 13 jz lab1 mov b0, al mov b1, 1 mov b, al mov bl, b jmp x3lab1: mov ah, 2 mov dl,13 int 21h mov dl,10 int 21h cmp a1,10 jb z1 mov ah,0 mov al, a1 mov bl, 10 div bl add ax, 3030h mov bx, ax mov ah, 2 mov dl, bl int 21h mov dl, bh int 21h jmp z2z1: add a1, 30h mov dl, a1 int 21h sub a1,30hz2: mov ah,2 mov dl, 13 int 21h mov dl, 10 int 21h mov cx, 0 mov cl, a1 mov ah, 2m: mov dl,a0 int 21h loop m mov ah, 4ch int 21hcode ends end main案例4加减乘除四则运算B_P EQU BYTE PTRW_P EQU WORD PTRD_P EQU DWORD PTRCODE SEGMENTASSUME CS:CODE,DS:CODE ORG 100H;主程序开始NEWSTAR: JMP STAR EVENNUM1 DW 0,0NUM2 DW 0,0NUM3 DW 0,0JGV4 DW 0,0YSF LABEL WORDYSF1 DB 0YSF2 DB 0JUV DB 0UV DB 0;使DS:SI指向第一有效字符OVERS LABEL NEAR CLD PUSH AXOVERS1: LODSB CMP AL,' ' JZ OVERS1 CMP AL,9 JZ OVERS1 DEC SI CMP AL,13 POP AX RET;输出一个制表符TAB LABEL NEAR MOV AL,9 JMP SHORT DISP;输出一个回车换行符CRLF LABEL NEAR MOV AL,13 CALL DISP MOV AL,10;显示一个字符DISP LABEL NEAR PUSH AX PUSH DX MOV AH,2 MOV DL,AL INT 21H POP DX POP AX RET;检查是否为运算符ISYSF LABEL NEAR CLD LODSB CALL ISJJ JZ ISYSF1 CALL ISII JZ ISYSF1 DEC SIISYSF1: RET;检查是否是加减运算ISJJ LABEL NEAR CMP AL,'+' JZ ISYSF1 CMP AL,'-' RET;检查是否为乘除运算ISII LABEL NEAR CMP AL,'*' JZ ISYSF1 CMP AL,'/' JZ ISYSF1 CMP AL,'' RET;数据输出;CX=10 十进制;CX=16 十六进制DOUT LABEL NEAR PUSH AX PUSH BX PUSH DX PUSH BP XOR BP,BPDOUT1: INC BP PUSH AX MOV AX,DX XOR DX,DX DIV CX MOV BX,AX POP AX DIV CX PUSH DX MOV DX,BX OR BX,AX JNZ DOUT1DOUT2: POP AX ADD AL,'0' CMP AL,'9' JBE DOUT3 ADD AL,'A'-'9'-1DOUT3: CALL DISP DEC BP JNZ DOUT2 POP BP POP DX POP BX POP AX RET;输入数据在DX:AX中返回;CX=0 数据为10进制;CX#0 数据为16进制DATE LABEL NEAR PUSH BX PUSH BP PUSH DI XOR AX,AX XOR DX,DXDATE1: MOV DI,AX LODSB CMP AL,'0' JB DATE7 CMP AL,'9' JA DATE6DATE2: AND AX,0FH SHL DI,1 RCL DX,1 MOV BX,DI MOV BP,DX SHL DI,1 RCL DX,1 SHL DI,1 RCL DX,1 JCXZ DATE3 SHL DI,1 RCL DX,1 Jmp short DATE4DATE3: ADD DI,BX ADC DX,BPDATE4: ADD AX,DI ADC DX,0 JMP DATE1DATE5: ADD AL,9 JMP DATE2DATE6: JCXZ DATE7 CMP AL,'A' JB DATE7 CMP AL,'F' JBE DATE5 CMP AL,'a' JB DATE7 CMP AL,'f' JBE DATE5DATE7: MOV AX,DI DEC SI OR DI,DX POP DI POP BP POP BX RET;数据1与数据2根据YSF1进行加减运算JSJJ LABEL NEAR MOV AX,NUM2 MOV DX,NUM2+2 CMP YSF1,'+' JZ JSJJ1 SUB NUM1,AX SBB NUM1+2,DX JMP SHORT JSJJ2JSJJ1: ADD NUM1,AX ADC NUM1+2,DXJSJJ2: RET;数据1与数据2根据YSF1进行乘除运算JSII1 LABEL NEAR MOV BX,OFFSET NUM1 JMP SHORT JSII2_1;数据2与数据3根据YSF2进行乘除运算JSII2 LABEL NEAR MOV BX,OFFSET NUM2JSII2_1: DB 66H MOV AX,BX DB 66H MOV CX,BX+4 CMP YSF2,'*' JNZ JSII2_2 DB 66H IMUL CX JMP SHORT JSII2_3JSII2_2: DB 66H CWD DB 66H IDIV CX CMP YSF2,'/' JZ JSII2_3 DB 66H XCHG DX,AXJSII2_3: DB 66H MOV BX,AX RET;显示结果XUJG LABEL NEAR MOV AX,JGV4 MOV DX,JGV4+2 MOV CX,10 CMP UV,10 JZ XUJG0 MOV CX,16XUJG0: TEST DX,8000H JZ XUJG1 CMP UV,10 JZ XUJG2 CALL DOUTXUJG2: NOT AX NOT DX ADD AX,1 ADC DX,0 PUSH AX CMP UV,10 JZ XUJG3 MOV AL,' ' CALL DISP MOV AL,'(' CALL DISPXUJG3: MOV AL,'-' CALL DISP POP AX CMP UV,10 JZ XUJG1 CALL DOUT MOV AL,')' CALL DISP RETXUJG1: CALL DOUT RET;计算结果放入DX:AX中JSJG LABEL NEAR CALL JSJJ MOV AX,NUM1 MOV DX,NUM1+2 RET;从DS:SI处取一个数据LOADATE LABEL NEAR LODSB CMP AL,'(' JZ LOADATE1 DEC SI PUSH CX XOR CX,CX CMP UV,10 JZ LOADATE0 INC CX ;取16进制数LOADATE0: CALL DATE ;取数据放入DX:AX中 POP CX RETLOADATE1: PUSH NUM1 ;保存数据1数据2及运算符 PUSH NUM1+2 PUSH NUM2 PUSH NUM2+2 PUSH YSF INC JUV MOV JGV4,0 ;结果值清零 MOV JGV4+2,0;进行四则运算SZYS LABEL NEAR CALL ISYSF ;首字符是运算符? JZ SZYS2 CALL LOADATE ;不是,取数并保存 MOV NUM1,AX MOV NUM1+2,DXSZYS1: XOR AX,AX ;数据2清零 MOV NUM2,AX MOV NUM2+2,AX CALL ISYSF ;下个是运算符? JZ SZYS3 JNZ SZYS6SZYS2: PUSH AX MOV AX,JGV4 ;将结果作为数据1 MOV NUM1,AX MOV AX,JGV4+2 MOV NUM1+2,AX POP AXSZYS3: MOV YSF1,AL MOV YSF2,AL CALL ISJJ ;是加减运算转 JZ SZYS4 CALL LOADATE ;取数据2 MOV NUM2,AX MOV NUM2+2,DX CALL JSII1 ;数据1与数据2根据YSF1进行乘除运算 JMP SZYS1 ; 结果保存在数据1中SZYS4: CALL LOADATE ;取数据2并保存 MOV NUM2,AX MOV NUM2+2,DXSZYS4_1: CALL ISYSF JNZ SZYS6 CALL ISJJ ;运算符2是加减运算? JNZ SZYS5 ;不是转 PUSH AX CALL JSJJ ;数据1与数据2根据YSF1进行加减运算 POP AX ; 结果保存在数据1中 MOV YSF1,AL ;保存新的运算符 JMP SZYS4SZYS5: MOV YSF2,AL ;保存运算符2 CALL LOADATE ;取数据3 MOV NUM3,AX MOV NUM3+2,DX CALL JSII2 ;数据2与数据3根据YSF2进行乘除运算 JMP SZYS4_1 ; 结果保存在数据2中SZYS6: MOV CL,AL CMP AL,13 JNZ SZYS9SZYS7: CALL JSJG CMP JUV,0 JZ SZYS8 DEC JUV POP YSF POP NUM2+2 POP NUM2 POP NUM1+2 POP NUM1 RETSZYS8: CMP CL,')' JZ SZYS10 MOV JGV4,AX MOV JGV4+2,DX JNZ SZYS12SZYS9: CMP AL,')' JNZ SZYS11 INC SI JMP SZYS7SZYS10: MOV NUM1,AX MOV NUM1+2,DX JMP SZYS1SZYS11: STC RETSZYS12: CLC RET;数制处理UVIL LABEL NEAR PUSH SIUVIL1: LODSB CMP AL,' ' JZ UVIL2 CMP AL,9 JZ UVIL2 CMP AL,13 JZ UVIL4 JNZ UVIL1UVIL2: MOV BX,SI CALL OVERS JZ UVIL3 LODSW CALL OVERS POP SI JNZ SZYS11 CMP AX,'01' JNZ SZYS11 PUSH SI MOV UV,10UVIL3: MOV AL,13 MOV BX-1,ALUVIL4: POP SI CLC RET;预置结果YVJG LABEL NEAR MOV AH,52H INT 21H MOV AX,ES:BX-2 MOV DS,AX MOV AX,DS:0CH MOV CS:JGV4,AX MOV AX,DS:0EH MOV CS:JGV4+2,AX PUSH CS POP DS PUSH CS POP ES RET;保存结果BCJG LABEL NEAR MOV AH,52H INT 21H MOV AX,ES:BX-2 MOV DS,AX MOV AX,CS:JGV4 MOV DS:0CH,AX MOV AX,CS:JGV4+2 MOV DS:0EH