试图在ASM中完成元音计数程序,但总是有非法指令"错误

时间:2018-02-02 02:03:18

标签: assembly dos x86-16 tasm

.model small
.stack 100h
.code
main proc
    mov ax,@data        ;initialize ds register
    mov ds,AX
    mov si,offset String    ;initialize p
    mov cx,len      ;length in CX register
    mov bl,00       ;vowel count=0
BACK:   mov al,[si]
    cmp al,'a'
    jb VOWEL
    cmp al,'z'      ;Convert the character to upper case
    ja VOWEL
    sub al,20H
VOWEL:  cmp al,'A'
    jnz a3
    inc bl
    jmp a2
a3: cmp al,'E'
    jnz a4
    inc bl
    jmp a2
a4: cmp al,'I'
    jnz a5
    inc BL
    jmp a2
a5: cmp al,'O'
    jnz a6
    inc bl
    jmp a2
a6: cmp al,'U'
    jmp a2
    inc bl
a2: inc si
    loop BACK
    mov si, ax
    mov ax, 4C00H
    int 21h
main endp
.data
    string db "Kono taisetsu na omoi wa wasurenai","$"
    len dw string
    **VOWEL db?** <- considers this as an illegal instruction
end main

1 个答案:

答案 0 :(得分:0)

未初始化的数据进入由data?表示的MASM中的BSS部分。这就是你得到非法指令错误的原因。

        .data?

Vowel:  db  ?, ?, ?             ; Reserves 3 bytes
        dw  ?, ?                ; Reserves 4 bytes 2 word
 Buff:  db  100 dup<?>          ; Reserves 100 bytes
  

这与你提出的问题无关,但你可能会发现它有用。

当DS:SI指向字符串时,您可以使用有助于此的X86指令,同时省去CX中的计数器。

    L0: lodsb                   ; Reads byte from DS:SI and increments SI
        cmp     al, '?'         ; Test if at end of string
        jz      Finished        ; You need a label called

        and     al, 5FH         ; Convert letter to uppercase
        cmp     al,'A'
        jz      Bump
        cmp     al,'E'
        jz      Bump

        ......

        cmp     al,'U'
        jnz     L0

 Bump:  inc     bl
        jmp     L0

Finished: