1

I want to write a assembly code that has 2 arrays and one variable then code search variable value into the first array if equal the index of this array write into the second array. i write this program but i have an issue . this code need double "inc DI" at the end but why?

.model small
.stack 100 
.data
   a dw 10 dup(4, 3, 4, 2, 8, 9, 4, 6, 7, 5)
   x dw 4
   b dw 10 dup(?)
.code
    main proc far 
        push DS               
        push 0                
        mov AX,@data          
        mov DS,AX             
        ;code start
        ;;;;;;;;;;
        mov si,0           
        mov di,0 
        startloop:
        cmp di,19
        jnl endloop
        mov ax,a[di]
        cmp ax,x
        jne endif
        mov b[si],di
        inc si
        endif:
        inc di
        inc di
        jmp startloop
        endloop:
ret
main endp
end main 
fuz
  • 88,405
  • 25
  • 200
  • 352
  • A word is two bytes. – Jester Dec 02 '17 at 21:35
  • yes . but when arrays and variable be byte (db) i have an issue with mov al,a[bl] (bl az si) – mohsen hasani Dec 02 '17 at 21:41
  • `bl` is 8 bit register. Memory addresses in 16b real mode need 16b offsets. The only legal memory operand combinations in 16b real mode are here: https://stackoverflow.com/a/12474190/4271923 plus learn the default segment registers (`bp` and `sp` does use `ss`, everything else is using `ds` by default). You will have to sort of memorize it, or have it printed on cheat-sheet paper, if you want to write some more of 16b x86 asm. ... About your question: you need to understand difference between data size (you can use 8, 16 and even 32b on 80386+ CPU in 16b real mode) and memory address (16b). – Ped7g Dec 02 '17 at 21:51
  • Actually the easiest way to understand your current situation is to load your code into debugger, step over first few instructions setting `ds, si, di`, and then check memory view at `ds:si` to understand what `dw` produces. From there it should be obvious, why address offset needs +2 for `word` elements. – Ped7g Dec 02 '17 at 21:55
  • ok , tnx . but my code is true legally ? – mohsen hasani Dec 02 '17 at 21:55
  • which one? `mov al,a[bl]` is never legal on x86. In 16b mode the closest thing to that is `mov al,a[bx]`. I prefer `mov al,[a + bx]` syntax style (to have all memory address values inside brackets), and longer labels which have some meaning, not "a" or "loop1" and similar. .. The code in question needs +2 also for `b` array addressing (it's words too), so 2x `inc si` ... or just `add si,2` `add di,2` – Ped7g Dec 02 '17 at 21:57
  • 1
    Also, you can quite easily extend a 8 bit number into 16 when you want to index. – Jester Dec 02 '17 at 22:27

0 Answers0