简体   繁体   中英

How do I use CMP in assembly to test a segment and offset address?

I want to leave a loop if the current address I'm looking at is at least 0xFFFF0. Here is the portion of code I wrote, but obviously does not work:

CMP DS:[BX], FFFF0H
JGE LeaveLoop

I'm very new to assembly and have not used CMP for anything more than simple number comparisons.

This sounds like an XY problem, you should have specified what you wanted to achieve ultimately.

Anyway, FFFF0H is a 20 bit address, you can't compare with that directly if you are limited to 16 bits. You can use two 16 bit registers to calculate the physical address, and do a 32 bit comparison using those.

    MOV AX, DS
    MOV DX, DS
    SHL AX, 4
    SHR DX, 12    ; DX:AX has segment base now
    ADD AX, BX    ; add offset
    ADC DX, 0     ; DX:AX has full address now
    CMP DX, 0Fh   ; high word has to be at least F
    JB  false
    JA  true      ; if it's more we are ok
    CMP AX, FFF0h ; low word has to be at least FFF0h
    JAE true
false:
    ...
true:
    ...
    MOV AX, BX
    MOV DX, DS
    SHR AX, 4    ; doesn't need lowest nibble
    ADD AX,DX    ; add offset
    JC LeaveLoop ; See note!!!
    CMP AX, FFFFh
    JE LeaveLoop
    ...
LeaveLoop:

Note: if overflow does NOT count, then JC LeaveLoop should be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM