1

I have a question about summing one-byte array to ax, this is my code:

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
    arr db 6 dup (200)
; --------------------------
CODESEG
start:
    mov ax, @data
    mov ds, ax
; --------------------------
    xor ax, ax
    add ax, [arr]
    add ax, [arr + 1]
    add ax, [arr + 2]
    add ax, [arr + 3]
    add ax, [arr + 4]
    add ax, [arr + 5]
; --------------------------

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


The assembler returns "Operand types do not match" I'm new to assembly so how can I overcome that? Thanks

rkhb
  • 14,159
  • 7
  • 32
  • 60
Liron
  • 49
  • 7
  • 1
    MASM is saving you from yourself: you wrote an add with a 16-bit memory source operand which would be 2 array elements taken as one 16-bit integer. Do a zero-extending load (like `movzx cx, [arr]`) and add that to AX. – Peter Cordes Dec 28 '19 at 14:22
  • When I do movzx it says "Illegal instruction for currently selected processor" (I work on the 8086) – Liron Dec 28 '19 at 14:35

1 Answers1

2

Eventually I did this to solve my problem:

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
    arr db 6 dup (200)
; --------------------------
CODESEG
start:
    mov ax, @data
    mov ds, ax
; --------------------------
    xor ax, ax
    xor bh, bh
    mov bl, [arr]
    add ax, bx
    mov bl, [arr + 1]
    add ax, bx
    mov bl, [arr + 2]
    add ax, bx
    mov bl, [arr + 3]
    add ax, bx
    mov bl, [arr + 4]
    add ax, bx
    mov bl, [arr + 5]
    add ax, bx
; --------------------------

exit:
    mov ax, 4c00h
    int 21h
END start
Liron
  • 49
  • 7
  • 1
    Yup, that's an efficient way to zero-extend when you have to avoid 80386 `movzx` for compat with truly ancient CPUs. – Peter Cordes Dec 29 '19 at 00:31
  • More efficient on some CPUs (especially Intel Pentium Pro through Nehalem) would be `xor bx,bx`, not `xor bh,bh`. That would hopefully avoid [partial-register merging penalties](https://stackoverflow.com/questions/41573502/why-doesnt-gcc-use-partial-registers) when reading BX after writing BL. On other CPUs, both ways will perform about equally, and same code size. – Peter Cordes Dec 09 '22 at 16:05