汇编语言实现通过8255A和4个开关控制实现8个LED灯和8个7位数码管显示指定数字全亮、全灭、从左至右、从右至左跑马灯式点亮
最后改进代码:
.MODEL TINY .STACK 100 .DATA ledshow DB 11111110B ;数据段定义变量按“变量名 数据类型 数据”;DB为字节型 seven DB 07h zero DB 00h .CODE START: A equ 0270H ;数码管端口地址 B equ 0271H ;0-1开关端口地址 C equ 0272H ;控制数码管显示内容 mov ax,@DATA ;获得当前程序的数据段地址,并传送给数据段DS寄存器保存。 mov ds,ax ;这样,程序中访问存储器操作数时,默认情况下就会从DS指定的数据段进行存取 mov dx,273h mov al,10000010H out dx,al function: call delay mov dx,B in al,dx ;从dx端口地址中将数据读入到al中 and al,00001111B cmp al,00000001B je fun1 cmp al,00000010B je fun2 cmp al,00000100B je fun3 cmp al,00001000B je fun4 fun1: mov dx,A mov al,00H out dx,al ;将al中的值读出到dx端口地址中 jmp function fun2: mov dx,A mov al,0FFH out dx,al mov dx,C mov al,01011011B out dx,al jmp function fun3: mov dx,C mov al,01001111B out dx,al mov dx,A fun3xh: mov cx,8 lea bx,ledshow rol ledshow mov al,seven dec seven xlat out dx,al call delay loop fun3xh jmp function fun4: mov dx,C mov al,01100110B out dx,al mov dx,A fun4xh: mov cx,8 lea bx,ledshow ror ledshow mov al,zero inc zero xlat out dx,al call delay loop fun4xh jmp function delay proc near ;proc为子程序起点; 调用程序和子程序在同一code segment中时用near, 只能被相同code segment的其它程序调用; 不在同一code segment中时用far,可以被相同或不同的代码段程序调用。 push cx mov cx,30000 delay1: loop delay1 pop cx ret ;子程序结束时调用, 用于实现相应地址参数从该子程序到下一步程序的转移的转移 delay endp END START
改进前汇编代码:
.MODEL TINY COM_ADD EQU 0273H PA_ADD EQU 0270H PB_ADD EQU 0271H PC_ADD EQU 0272H .STACK 100 .DATA LED_Data DB 11111110B DB 11111101B DB 11111011B DB 11110111B DB 11101111B DB 11011111B DB 10111111B DB 01111111B .CODE START: MOV AX,@DATA MOV DS,AX NOP MOV DX,COM_ADD MOV AL,82H ;PA、PB、PC为基本输出模式 OUT DX,AL LOOP1: CALL DL500ms CALL DL500ms MOV DX,PB_ADD IN AL,DX AND AL,00001111B CMP AL,00000001B JE f1 cmp al,00000010B je f2 cmp al,00000100B je f3 cmp al,00001000B je f4 f1: mov dx,PA_ADD MOV al,00H OUT dx,al jmp LOOP1 f2: mov dx,PA_ADD MOV al,0FFH OUT dx,al mov dx,PC_ADD mov al,01011011B out dx,al jmp LOOP1 f4: mov dx,PC_ADD mov al,01100110B out dx,al mov dx,PA_ADD lea bx,LED_DATA mov al,0 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,1 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,2 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,3 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,4 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,5 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,6 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,7 xlat out dx,al call DL500ms jmp LOOP1 f3: mov dx,PC_ADD mov al,01001111B out dx,al mov dx,PA_ADD lea bx,LED_DATA mov al,7 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,6 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,5 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,4 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,3 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,2 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,1 xlat out dx,al call DL500ms lea bx,LED_DATA mov al,0 xlat out dx,al call DL500ms jmp LOOP1 DL500ms PROC NEAR PUSH CX MOV CX,30000 DL500ms1: LOOP DL500ms1 POP CX RET DL500ms ENDP END START
前导题目:
LEDdt db 8 dup(0) mov si, offset LEDdt call LEDdisp LEDdisp proc push ax push bx push dx mov bx, offset LEDtb mov ah, 0feh LED1: lodsb xlat cs:LEDtb mov dx, segport out dx, al mov al, ah mov dx, bitport out dx, al call delay rol ah, 1 cmp ah, 0feh jnz LED1 pop dx pop bx pop ax ret LEDtb db 0c0h, 0f9h, 0a4h, 0b0h, 99h, 92h, 82h, 0f8h db 80h, 90h, 88h, 83h, 0c6h, 0c1h, 86h, 8eh LEDdisp endp timer =10 delay proc push bx push cx mov bx, timer delay1: xor cx, cx delay2: loop delay2 dec bx jnz delay1 pop cx pop bx ret delay endp