[SOLVED]How to use the CMP instruction

Programming, for all ages and all languages.
Post Reply
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

[SOLVED]How to use the CMP instruction

Post 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?
Last edited by bashcommando on Tue Jan 20, 2015 3:48 pm, edited 1 time in total.
Building an operating system is like building an airplane, you don't want it to crash.
AbstractYouShudNow
Member
Member
Posts: 92
Joined: Tue Aug 14, 2012 8:51 am

Re: Help with my OS

Post 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
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: Help with my OS

Post 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?
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: Help with my OS

Post 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:
Building an operating system is like building an airplane, you don't want it to crash.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: [SOLVED]How to use the CMP instruction

Post 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.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: [SOLVED]How to use the CMP instruction

Post 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.
Building an operating system is like building an airplane, you don't want it to crash.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: [SOLVED]How to use the CMP instruction

Post by Combuster »

Why would you even want to compare two constants in the first place?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bashcommando
Member
Member
Posts: 62
Joined: Mon Jan 06, 2014 4:23 pm

Re: [SOLVED]How to use the CMP instruction

Post 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.
Building an operating system is like building an airplane, you don't want it to crash.
Post Reply