Home > database >  How to search for 8-bit value in a memory area and output the memory address? SBC86 Assembly
How to search for 8-bit value in a memory area and output the memory address? SBC86 Assembly

Time:01-27

I have to create a program for university that searches for a previously specified 8-bit value in the memory area C000H-CFFFH (monitor program of the SBC86). We use an i8086 emulator.

But I have absolutely no idea how exactly I search the memory area and output the memory address of the value found.

CodePudding user response:

You can use REPNE SCASB to search for the known value. We'll assume that value is currently in AL (if it's not, mov it there first.)

REPNE SCASB (repeat if not equal, scan string byte) works like this:

  1. Compare AL to byte ptr [es:di] (the byte at the memory address specified by di) and set the flags accordingly.
  2. Add 1 to di (if direction flag is clear, otherwise subtract 1) and subtract 1 from cx, without altering the flags.
  3. If AL = byte ptr [es:di], OR cx = 0, move to the next instruction.
  4. Otherwise, goto 1.
cld            ;we want scasb to auto-inc
mov di,0C000h  ;begin search at $C000 (assumes ES = segment you want to search)
mov cx,1000h   ;repeat until $CFFF
repne scasb    ;begin the search

At this point, di will contain the address of the byte, if it was found. However, in this case you still need a conditional branch, for the corner case where the desired byte was at the last possible position. Looking at di alone won't tell you that.

cld
mov di,0C000h  
mov cx,1000h   
repne scasb    
jne notFound 
    ;your code for what happens when you find it goes here
    jmp done
notFound:
    ;your code for what happens when you don't find it goes here
done:
ret

EDIT: Changed the incorrect usage of ds:si to es:di in accordance with the hardware specifications.

  • Related