【问题标题】:Increment an array in assembly在程序集中增加一个数组
【发布时间】:2013-10-07 21:53:48
【问题描述】:

编写汇编程序以用作字典。用户输入单词,程序检查该单词是否存在。 TASM,16 位。程序非常适合数组的前两个元素,但是如果我提供数组的第三个元素 balls. ,即使正在选择数组的下一个元素,(在 emu8086 上验证,bx 变为 @987654324 @,-> 参考代码repe cmpsb 在一次尝试后仍然完成检查。 适用于数组的前两个元素。这是我的代码 该程序首先检查长度,然后检查位。提供句点 (.) 时结束长度检查。

.model large
.data
arrayele db 00d ;to count if all elements of the array have been compared with
count db 00d ;length of input count
nl db 10d,13d,'$' ;newline
mne db "Not Equal$" ;message if not equal
me db "Equal$" ;message if equal
buf db 99,?,99 dup(?) ;buffer where the input will be saved
w0 db "hello$" ;word 0-5
w1 db "which$"
w2 db "balls$"
w3 db "table$"
w4 db "chair$"
w5 db "apples$"
words dw offset w0,offset w1 ;the array
      dw offset w2,offset w3
      dw offset w4,offset w5
.code

main proc
    mov ax,@data
    mov ds,ax
    mov es,ax

    ;take user input
    mov ah,0ah
    mov dx,offset buf
    int 21h

    ;print new line
    mov ah,09h
    mov dx,offset nl
    int 21h

    ;load input to di
    mov di,offset buf
    add di,2

    ;//saving length to a variable
    repeat:
    mov al,[di]
    inc count
    cmp al,"."
    je lenchck
    inc di
    jmp repeat
    ;//end saving
    lenchck: 

    dec count  ;as full stop (period) (.) is also included in the count

    stringmatch1:
    mov cx,0  ;reset register
    mov arrayele,0 ;reset variable
    stringmatch: 
    mov di,offset buf  ;loading input to di
    add di,2
                          ;loading array element to si
    mov bx,0
    mov bl,byte ptr words
    mov si,bx             ;end loading array element to si
    mov cl,count
    repe cmpsb
    je equal

    inc arrayele
    inc words    ;next word in the array
    mov bx,0         ;loading it
    mov bl,byte ptr words
    mov si,bx        ;end loading
    cmp arrayele,06d      ;compare to check if all elements have been compared with
    jg wrong

    jmp stringmatch

    wrong:                ;load notequal message
    mov dx,offset mne
    jmp print

    equal:
    mov dx,offset me      ;load equal message
    print:
    mov ah,09h            ;print it    
    int 21h

    mov ah,4ch            ;exit the program
    int 21h
main endp
end main

【问题讨论】:

  • 字典将超过近 200 个单词,所以是的,数组是必须的,别无他法:)
  • buf db 99,?,99 dup(?) 的目的是什么(与 buf db 99 dup(?) 相比)?
  • @us2012 用于输入,必须这样做,否则程序不会输入

标签: arrays assembly x86 tasm


【解决方案1】:

inc words ;next word in the array

不,对不起。你已经增加了(什么是)w0 的偏移量。您还没有移动到数组中的下一个单词。你想要的东西更像...

mov bx, offset words
top:
; get offset of a string
mov si, [bx]
; do your comparison
; start at beginning of input every time
mov di, offset buf + 2 ; don't need a separate operation
mov cl, count ; (are we sure ch is clear?)
repe cmpsb
je found
; no? get next offset in words array
add bx, 2
jmp top

在寻找“.”的循环中,如果讨厌的用户不输入“.”会发生什么?中断返回后,输入缓冲区中的第二个字节 (buf + 1) 是实际输入的计数。它包括“。” (如果有)和结束输入的回车(13 或 0Dh)。您或许可以使用此信息来排除明显错误的输入。

【讨论】:

  • 感谢 frank,实施了回车政策,现在正在解决第一个问题 :)
  • 一个问题,我的数组words 已经存储了偏移量,为什么我要再次将它偏移到bx,因为您的代码状态mov bx,offset words
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
相关资源
最近更新 更多