Home > Blockchain >  what is wrong with my code it sayed that invalid combination of opcode and operand
what is wrong with my code it sayed that invalid combination of opcode and operand

Time:11-03

here is my code

    [ORG 0x0100]
    
    jmp  start
    
    even: dw 16,18,20,22,24,26,28,30,32    
    avg:  dw 0
    
average:     
a1: add  ax, [bx]    
    add  bx, 2    
    loop a1    
    ret
    
start:    
    mov ax,0    
    mov bx,even    
    mov cx,8    
    call average    
    mov bx,9    
    div ax    
    mov avg,ax    
    mov ax, 0x4c00  ;terminate program    
    int 0x21

I do not know why I am getting this error.

CodePudding user response:

invalid combination of opcode and operand

If this is NASM, then mov avg,ax needs to become mov [avg], ax to avoid the error.
If this were MASM, then mov bx,even needs to become mov bx, OFFSET even for correct results.

You got errors!

  • The array has 9 elements but you process only 8 with mov cx,8.
  • Eventhough you prepare for a division by 9 with mov bx,9, you execute div ax which is wrong.
  • The word-sized division requires zeroing DX beforehand
start:    
    mov ax, 0    
    mov bx, even    
    mov cx, 9    
    call average    
    mov bx, 9
    xor dx, dx    
    div bx
    mov [avg], ax    
    mov ax, 0x4C00  ;terminate program    
    int 0x21
  • Related