Join 136,055 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,626 people online right now. Registration is fast and FREE... Join Now!
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
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.
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
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
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.
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.