8086 Assembly - Comparing Numbers

Comparing two numbers and outputing the lowest one

Page 1 of 1

6 Replies - 30562 Views - Last Post: 29 July 2008 - 11:31 AM

#1 I3AnThRaX69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-April 08

8086 Assembly - Comparing Numbers

Posted 24 July 2008 - 09:54 PM

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

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.

Is This A Good Question/Topic? 0
  • +

Replies To: 8086 Assembly - Comparing Numbers

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: 8086 Assembly - Comparing Numbers

Posted 24 July 2008 - 11:55 PM

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.


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.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5395
  • View blog
  • Posts: 27,389
  • Joined: 10-May 07

Re: 8086 Assembly - Comparing Numbers

Posted 25 July 2008 - 12:01 AM

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
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: 8086 Assembly - Comparing Numbers

Posted 25 July 2008 - 12:12 AM

View Postno2pencil, on 25 Jul, 2008 - 12:31 PM, said:

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! :^:

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.
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: 8086 Assembly - Comparing Numbers

Posted 25 July 2008 - 12:20 AM

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:
  • JE or JZ (Jump when equal/zero)
  • JNE or JNZ (Jump when not equal/zero)
  • JG or JA (Jump when greater/above)
  • JL or JB (Jump when lesser/below)
  • JGE or JAE
  • JLE or JBE
  • ..other jumps on carry conditions etc. which you won't need for this
You can use the jumps like this:
   ; 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

:)
Was This Post Helpful? 1

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: 8086 Assembly - Comparing Numbers

Posted 25 July 2008 - 12:27 AM

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.
Was This Post Helpful? 0
  • +
  • -

#7 I3AnThRaX69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-April 08

Re: 8086 Assembly - Comparing Numbers

Posted 29 July 2008 - 11:31 AM

Thank you all for your help, the information that you provided helped a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1