Page 1 of 1

[SOLVED]How to use the CMP instruction

Posted: Tue May 20, 2014 7:19 am
by bashcommando
I have a problem with my code. I don't know how to debug assembly. I have a problem with the

Code: Select all

cmp
command. Here is the code:

Code: Select all

    BITS 16

start:
    in al, 0x92
    test al, 2
    jnz skip
    or al, 2
    and al, 0xFE
    out 0x92, al
    jmp skip

skip:
    mov ax, 07C0h
    add ax, 288
    mov ss, ax
    mov sp, 4096
    mov ax, 07C0h
    mov ds, ax
    mov si, welcome
    call print_si
    jmp term
    welcome db 'Welcome to starfruit', 0

print_si:
    mov ah, 0Eh

.repeat:
    lodsb
    cmp al, 0
    je .done
    int 10h
    jmp .repeat

.done:
    ret
    times 510-($-$$) db 0
    dw 0xAA55

term:
    int 80h
    [color=#FF0000]cmp 80h, 0x65[/color]
    je .done
    [color=#FF0000]cmp 80h, 0x76[/color]
    je vdisplay
    [color=#FF0000]cmp 80h, 0x62[/color]
    je boothdd
    jmp error

error:
    mov si, emsga
    call print_si
    mov si, emsgb
    jmp term
    emsga db 'Commands are 1 byte long', 0
    emsgb db 'Try v, b, or e', 0

vdisplay:
    mov si, version
    call print_si
    jmp term
    version db 'Version 1.00', 0

boothdd:
    mov si, haltmsg
    call print_si
    hlt
    haltmsg db 'System Halt', 0
I highlighted the lines NASM told me was the problem. But I don't see a problem(I'm new to assembly).
This is how I build it:

Code: Select all

nasm -f bin -o boot.bin boot.asm
dd status=noxfer conv=notrunc if=boot.bin of=floppy.flp
mkisofs -o disc.iso -b floppy.flp
Help?

Re: Help with my OS

Posted: Tue May 20, 2014 7:49 am
by AbstractYouShudNow
You added code after the end. Everything after 0xAA55 is not included in the bootsector, so you jump to a location that contains random values, which causes an exception, which ultimately results in the pc rebooting

Re: Help with my OS

Posted: Tue May 20, 2014 7:56 am
by Bender

Code: Select all

int 80h
?
cmp 80h, 0x65
So you're trying to check if 0x80 = 0x65? Do they? :)
The CMP instruction does exactly what it says it compares two operands and sets the (E/R)FLAGS accordingly. Like,
cmp ax, 7 -- Is AX = 7?
If AX is equal to 7 IIRC the ZF (Zero Flag) will be set, and you can use JZ or JE.
JE/JZ AddressX -- Jump to AddressX if ZF set

Have a look at the x86 jump instructions here: http://www.unixwiz.net/techtips/x86-jumps.html
Similarly, you can use:
cmp ax, [7] -- Is AX = value at address 7?

Re: Help with my OS

Posted: Tue Jan 20, 2015 2:02 pm
by bashcommando
AbstractYouShudNow wrote:You added code after the end. Everything after 0xAA55 is not included in the bootsector, so you jump to a location that contains random values, which causes an exception, which ultimately results in the pc rebooting
I at least know how to reboot it now. :lol:

Re: [SOLVED]How to use the CMP instruction

Posted: Tue Jan 20, 2015 5:40 pm
by eryjus
I recommend you take a look at this reference. It has everything you ever wanted to know about programming for an Intel processor, including the instruction set reference for CMP.

Re: [SOLVED]How to use the CMP instruction

Posted: Wed Jan 21, 2015 8:29 am
by bashcommando
eryjus wrote:I recommend you take a look at this reference. It has everything you ever wanted to know about programming for an Intel processor, including the instruction set reference for CMP.
Thanks but I already figured it out, you can't use cmp with 2 hexadecimal values without storing it in a variable.

Re: [SOLVED]How to use the CMP instruction

Posted: Wed Jan 21, 2015 10:08 am
by Combuster
Why would you even want to compare two constants in the first place?

Re: [SOLVED]How to use the CMP instruction

Posted: Wed Jan 21, 2015 10:53 am
by bashcommando
Combuster wrote:Why would you even want to compare two constants in the first place?
You see, a year ago I thought 0x80 was a memory location not a constant. I needed to use a interrupt, then again I wasn't using linux so int 80h would not have worked anyways.