Comparing two numbers in M86k assembly

This is a discussion on Comparing two numbers in M86k assembly within the Tech Board forums, part of the Community Boards category; I'm trying to write a min-sort subroutine, but the CMP instruction has me confused. Does it do overflow checking for ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Comparing two numbers in M86k assembly

    I'm trying to write a min-sort subroutine, but the CMP instruction has me confused. Does it do overflow checking for you, or do I need to check for overflows and then flip the result on overflow? The documentation that I have to work with is not very good to say the least.

    Have I got this right:

    signed (pseudocode)
    Code:
    CMP src, dest
    if Z=1, jump to _end_
    if V=0, jump to _sort_
    EXG src,dest
    _sort_:
    if N=1, jump to _end_ ; dest>src
    EXG src,dest
    _end_:
    ; the register situation will be: src<=dest
    I'm not quite sure how to handle the unsigned representation ...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It works exactly like subtract, except the "destination" register is not modified.

    It's been a long time since I wrote 68K assembler, but I think this is right:
    Code:
     
        cmp  D1, D0
        bge  _end
        exg  D1, D0
    
    _end:
        ret
    I you may need to swap the BGE for a BLE instruciton.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Doh, I should've looked at the branch jumps first. You use BGE for signed and BCC for unsigned. So yeah, your program will work for signed numbers.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Memloop View Post
    Doh, I should've looked at the branch jumps first. You use BGE for signed and BCC for unsigned. So yeah, your program will work for signed numbers.
    Yes, and I beleive from your original post that you wanted a signed conversion - otherwise, why are you trying to look at the N bit of the condition code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What do you mean? The branch jump for BGE is N exor V = 0, so surely you must check the N flag?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. C/C++ vs assembly: speed comparison
    By Just in forum C++ Programming
    Replies: 11
    Last Post: 11-25-2002, 03:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21