assembly 如何检查逮捕是否呈“波浪状”?

w8ntj3qf  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(103)

我的代码应该检查ARR是否是“波浪形”,这意味着第一个元素小于第二个元素,第二个元素大于第三个元素,直到ARR结束都是这样。但它不起作用。代码如下:

IDEAL
MODEL small
STACK 100h
DATASEG

        
ARR db 3 dup (?)
REZ db 1

CODESEG
start:
    mov ax,@data
    mov ds,ax
    mov cx,3                    ;cx=99
    xor ax,ax                    ; ax=0
    xor si,si                    ;si=0
    
    mov [ARR],0
    mov [ARR+1], 1
    mov [ARR+2], 0
    lea bx,[ARR]                 ; bx=offset arr
    L1: cmp cx,1
        je finish
        mov di,cx                ;di=cx (as index)
        neg di                   ;di=-di
        lea si,[bx+di] 
        mov ax,[3+si]
        cmp ax,[4+si]      ; compre the odd vs even index                                                   illegal use of register
        jg wrong                             ; exit if the odd index > even index
        dec cx                               ; cx=cx-1
        cmp cx,0                             ; check if cx=0
        je finish                            ; if  cx=0 finish
        mov di,cx                ;di=cx (as index)
        neg di                   ;di=-di
        lea si,[bx+di] 
        mov ax,[3+si]
        cmp ax,[4+si]        ; compre the even vs odd index                                                 illegal use of register
        jl wrong                            ; exit if the even <odd index
        loop L1                             ; cx=cx-1      if cx!=0 -> jump to L1
    wrong:
        mov [REZ],0
    finish:
    

exit:
    mov ax,4c00h
    int 21h
END start

there is even an example but it doesn't work..
do u know where is the mistake?

最后,如果arr是“波状的”,我们应该以res=1结束,或者如果它不是波状的,arr=0

k0pti3hp

k0pti3hp1#

数组元素的大小(byte)与对这些元素执行的操作的大小(word)不匹配。
定义ARR db 3 dup (?)与代码mov ax,[3+si]cmp ax,[4+si]不匹配。您需要改为编写mov AL, [3+si]cmp AL, [4+si]
loop结束时,你不应该陷入 wrong,而是jmp进入 exit
单个地址寄存器就足够了:

sub  cx, 1
  jbe  exit         ; In case of 0 or 1 array elements
  lea  si, [ARR]
  cld               ; Clear direction flag so LODSB will increment SI
L1:
  lodsb
  cmp  al, [si]
  jg   wrong
  dec  cx
  jz   exit
  lodsb
  cmp  al, [si]
  jl   wrong
  loop L1
  jmp  exit
wrong:
  ...
exit:
  ...

相关问题