Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,815 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,176 people online right now. Registration is fast and FREE... Join Now!




8086 Assembly - Comparing Numbers

 
Reply to this topicStart new topic

8086 Assembly - Comparing Numbers, Comparing two numbers and outputing the lowest one

I3AnThRaX69
24 Jul, 2008 - 08:54 PM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 4


My Contributions
I've been trying to do this assignment for my class online and the teacher is no help, being a repetitive drone repeating "read the assigned readings" even when I've told him numerous times that I have.

BUT the problem that I have is that I'm trying to make this very basic programing under assembly for 8086 that will prompt for 2 different numbers and output the lowest number regardless of which was entered first. here is my code...

CODE

include 'emu8086.inc'

org  100h; set location counter to 100h

jmp CodeStart

DataStart:
    promptMsg db "enter a number> ", 0
    minMsg db "the smallest number is ", 0
    promptMsgtwo db "enter the second number> ", 0
    newline db 13, 10, 0
    num1 dw ?
    num2 dw ?
    numSmall dw ?

CodeStart:
  
   ; prompt user to enter in a number
    mov si, offset promptMsg
    call print_string
  
   ; read in the number into cx
    call scan_num
  
   ; move the number to a variable
    mov num1, cx
  
   ; advance cursor to the next line
    mov si, offset newline
    call print_string

   ; prompt user to enter in a number
    mov si, offset promptMsgtwo
    call print_string
  
   ; read in the number into cx
    call scan_num
  
   ; move the number to a variable
    mov num2, cx
  
   ; advance cursor to the next line
    mov si, offset newline
    call print_string
  
   ;compare values
    CMP num1, num2
                  
   ; print min message
    mov si, offset minMsg
    call print_string

    mov ax, numSmall

    call print_num  
  
    EndLabel:
    ret
  
DEFINE_PRINT_STRING  
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS


Where it says
;compare values
CMP num1, num2
I keep getting the error wrong parameters on line 47 (for those of you who will actually put it in an 8086 emulator). The main problem I'm having is that I'm going to have to cause some form of a "jump" after wards. But since I can already tell that I'm not using the statement CMP correctly, how should I be calling it. And how would I be able to jump to two different locations (if need be) to be able to say which one is the lowest if number 1, or number 2 is lower.

Please someone help with this, and if you do thank you for your help.
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: 8086 Assembly - Comparing Numbers
24 Jul, 2008 - 10:55 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
I think you need conditional jumps after CMP. I haven't done 8086 for last few years but I think I can give you basic logic about how you can do it.

As far as I know CMP supports only there formats
REG, memory
memory, REG
REG, REG
memory, immediate
REG, immediate

So you can't compare Memory to memory using CMP. You can copy one of your numbers in register and then compare.

CODE


include 'emu8086.inc'

org  100h; set location counter to 100h

jmp CodeStart

DataStart:
    promptMsg db "enter a number> ", 0
    minMsg db "the smallest number is ", 0
    promptMsgtwo db "enter the second number> ", 0
    newline db 13, 10, 0
    num1 dw ?
    num2 dw ?
    numSmall dw ?

CodeStart:
  
  ; prompt user to enter in a number
    mov si, offset promptMsg
    call print_string
  
  ; read in the number into cx
    call scan_num
  
  ; move the number to a variable
    mov num1, cx
  
  ; advance cursor to the next line
    mov si, offset newline
    call print_string

  ; prompt user to enter in a number
    mov si, offset promptMsgtwo
    call print_string
  
  ; read in the number into cx
    call scan_num
  
  ; move the number to a variable
    mov num2, cx
  
  ; advance cursor to the next line
    mov si, offset newline
    call print_string

  ; print min message
    mov si, offset minMsg
    call print_string

  ;copy num1 in BX so we can compare
   mov bx, num1 ;I used bx here with random thought
  ;it is not necessary that you should use same register.
  
  ;compare values
    CMP bx, num2
  ;I think this will set CF in flags if num1 is less than num2,
  ; I am not sure, it can be exactly reverse also, you just need to
  ; change your print statement in that case

   JB minnum1            
  
    mov ax, num2 ;it is here because num1 is greater than num2
    JMP printnum  ; go to print minimum number

minnum1:
    mov ax, num1 ;it is here because num1 is smaller than num2

printnum:
    call print_num  
  
    EndLabel:
    ret
  
DEFINE_PRINT_STRING  
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS



I hope this will help you.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: 8086 Assembly - Comparing Numbers
24 Jul, 2008 - 11:01 PM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,480



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Your jumps for x86 are going to be :

je <label> - Jump when equal
jne <label> - Jump when not equal
jz <label> - Jump when last result was zero
jg <label> - Jump when greater than
jge <label> - Jump when greater than or equal to
jl <label> - Jump when less than
jle <label> - Jump when less than or equal to
User is online!Profile CardPM
+Quote Post

AmitTheInfinity
RE: 8086 Assembly - Comparing Numbers
24 Jul, 2008 - 11:12 PM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(no2pencil @ 25 Jul, 2008 - 12:31 PM) *

Your jumps for x86 are going to be :

je <label> - Jump when equal
jne <label> - Jump when not equal
jz <label> - Jump when last result was zero
jg <label> - Jump when greater than
jge <label> - Jump when greater than or equal to
jl <label> - Jump when less than
jle <label> - Jump when less than or equal to



Absolutely! icon_up.gif

I think JE and JZ, JL and JB, JG and JA are considered same after CMP [selection of them will be based on whether the numbers you are comparing are signed or unsigned] as CMP is nothing but the subtract second parameter from the first.]

If you need any more info on 8086, you can see here.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: 8086 Assembly - Comparing Numbers
24 Jul, 2008 - 11:20 PM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,907



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
CMP is nothing but the subtract second parameter from the first.

Yes, but the CF, OF, SF, ZF, AF, and PF flags are set according to the result.


You will need to store the contents of num1 and num2 into registers and then compare them with CMP.

You will need to use any of the conditional jump instructions that the 8086 supports such as:
  1. JE or JZ (Jump when equal/zero)
  2. JNE or JNZ (Jump when not equal/zero)
  3. JG or JA (Jump when greater/above)
  4. JL or JB (Jump when lesser/below)
  5. JGE or JAE
  6. JLE or JBE
  7. ..other jumps on carry conditions etc. which you won't need for this
You can use the jumps like this:
asm

; print min message
mov si, offset minMsg
call print_string

mov ax, num1
mov bx, num2

;compare values
CMP ax, bx
JG num2IsLesser

mov ax, num1
jmp printfinal

num2IsLesser:
mov ax, num2

printfinal:
call print_num

EndLabel:
ret

smile.gif
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: 8086 Assembly - Comparing Numbers
24 Jul, 2008 - 11:27 PM
Post #6

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
As I said earlier, he is getting error for CMP because he is trying to compare Memory with memory whereas CMP supports only these formats

Compare REG to memory
Compare memory to REG
Compare REG to REG
Compare memory to immediate
Compare REG to immediate

Now to overcome this error he has to copy one of the numbers from memory to register and then compare, I don't see any other big problem in his code. But as I said, I am out of touch from Assembly for few years so I might be missing something too.
User is offlineProfile CardPM
+Quote Post

I3AnThRaX69
RE: 8086 Assembly - Comparing Numbers
29 Jul, 2008 - 10:31 AM
Post #7

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 4


My Contributions
Thank you all for your help, the information that you provided helped a lot.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 02:43PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month