8086汇编常见问题及解决方案

(乱序)
附: 8086汇编,俄罗斯方块完整代码:
https://github.com/Dulun/Assembly

1.低级错误

忘记设置data段

mov ax,data
mov ds, ax

2.键盘输入&扫描码

equ:宏替换,

$left   equ 75
$right  equ 77
$quit   equ 113

int 16h 中断:从键盘缓冲区读取数据
入口参数:AH=00H——读键盘
出口参数:ZF=1——无字符输入,否则,AH=键盘的扫描码,AL=ASCII码。

mov ax, 0h
int 16h
cmp al,'a'       ;ascii码
...
cmp ah, 75        ;扫描码(上下左右esc)

3.文件读写

data
...
    path1           db 'c:/score.txt'
    buffer1         db 0
    buffer2         db 49
...

data ends

read_score proc near

    mov ax, data
    mov ds, ax

    ;打开文件TEST.TXT
    mov ah,3DH
    lea dx,path1
    mov al,2
    int 21H
    jc gones


    ;读取文件内容,存入buffer1
    lea dx,buffer1
    mov bx,ax
    mov cx,1
    mov ah,3FH
    int 21H

    ; ;显示buffer1中的内容
    ; mov cx,ax
    ; lea si,buffer1
    ; NEXT:
    ; mov dl,[si]
    ; mov ah,2
    ; int 21H
    ; INC si
    ; LOOP NEXT
    xor ax,ax
    mov al,buffer1[0]


gones:
    ret

    ;关闭文件
    mov ah,3EH
    int 21H

    ret


read_score endp
;-----------------------------------------------------------------------------------------
write_score proc near
    mov ax, data
    mov ds, ax

    xor ax, ax
    xor bx,bx
    mov al,buffer1[0]
    mov score[0],0

    cmp al,score[0]
    jbe oover
    xor ax, ax

    ;打开文件TEST.TXT
    mov ah,3DH
    lea dx,path1
    mov al,2
    int 21H
    jc oover
    ; mov dx,200H
    ; mov bx,0
    ; mov ds,bx
    mov dx,offset score

    mov bx,ax
    mov cx,1
    mov ah,40H
    int 21H

    mov ax, data
    mov ds, ax
    mov ah,3EH
    int 21H
oover:
    ret

write_score endp

4.两重循环,死循环,假sleep

for(int i = 0; i < 100; i++)
    for(int j = 0; j < 2; j++)

—->

    mov cx 100
loop_out:
    push cx
    mov cx, 2

    loop_in:
        mov ax, 0 ;~.~
        loop loop_in;

    pop cx
    loop loop_out

sleep:

mov cx,0fh ; speed !!!!!!

l:
    push cx
    mov cx,0ffffh
lo:
    mov ax,0
    loop lo

    pop cx
    loop l

5.je, jne, jz太远

je 先跳A,再在A中jmp B 或 call B

6.屏幕&输出

坐标:默认,左上角0B800F, 80列 * 25行

在左上角打印:

mov ax, 0b800h
mov es, ax

xor ax,ax
mov ax,data    ;设置数据段
mov ds,ax

xor bx, bx
xor si, si
mov ah,'@'
mov byte ptr es:[bx + si],ah

Hello World!

DATA SEGMENT
str db 'Hello World$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
       MOV BX,DATA
       MOV DS,BX
       LEA DX,str
       MOV AH,9
       INT 21H
       MOV AH,4CH
       INT 21H
CODE ENDS
END START

7.结束退出:

mov ax, 4c00h
int 21h

8.颜色:

设置时候,直接用二进制设置,
“荧光绿”:

mov al, 'A'
mov ah, 00001010b
mov es:[si], AL  ;低位字符
mov es:[si+1], AH;高位颜色

8086汇编常见问题及解决方案_第1张图片

9.随机数:

in  al,40h              ;开始随机选择方块类型,al=时间随机值
and al,0111b            ;通过与运算,控制范围

;al为随机数,范围:0~7 

10.时间:

时分秒,分别显示

show_time  proc near

    mov al,4
    out 70h,al
    in al,71h
    mov ah,al
    mov cl,4
    shr ah,cl
    and al,00001111b
    add ah,30h
    add al,30h

    mov bx,0b800h
    mov es,bx

    mov byte ptr es:[160*12 + 55*2  ],ah
    mov byte ptr es:[160*12 + 55*2 + 2],al
;--------
    xor ax,ax
    mov ah,':'
    mov al,02h
    mov byte ptr es:[160*12 + 57*2],ah
    mov byte ptr es:[160*12 + 57*2 + 1],al
    xor ax,ax

    mov al,2
    out 70h,al
    in al,71h
    mov ah,al
    mov cl,4
    shr ah,cl
    and al,00001111b
    add ah,30h
    add al,30h

    mov bx,0b800h
    mov es,bx

    mov byte ptr es:[160*12 + 59*2 ],ah
    mov byte ptr es:[160*12 + 59*2 + 2],al
;--------
    xor ax,ax
    mov ah,':'
    mov al,02h
    mov byte ptr es:[160*12 + 61*2],ah
    mov byte ptr es:[160*12 + 61*2 + 1],al
    xor ax,ax

    mov al,0
    out 70h,al
    in al,71h
    mov ah,al
    mov cl,4
    shr ah,cl
    and al,00001111b
    add ah,30h
    add al,30h

    mov bx,0b800h
    mov es,bx

    mov byte ptr es:[160*12 + 63*2 ],ah
    mov byte ptr es:[160*12 + 63*2 + 2],al


    ret

show_time endp

附: 8086汇编,俄罗斯方块完整代码:
https://github.com/Dulun/Assembly

你可能感兴趣的:(8086汇编)