News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Comparing two numbers problem

Started by infoMASM, March 14, 2014, 06:20:31 AM

Previous topic - Next topic

infoMASM

Hey everyone I know this might not be a difficult problem but I have been having a hard time with assembly language and keep doing this wrong. All I have to do is Implement the following C++ code in assembly language. Substitute calls to WriteString for the printf() function calls.
I then will run the program several times with different ranges of values for x and y to test the programs logic. But first I need to get it to work right.

If anyone can help I would really appreciate it! Thank you!!

C code:
double X;
double Y;
if( X < Y )
      printf("X is lower\n");
else
      printf("X is not lower\n")

This is what I have in assembly:

;C code origional:
; double X
; double Y
; if(X < Y)
; printf("X is lower\n")
; else
; printf("X is not lower\n")
;

INCLUDE Irvine32.inc

.data
mssg1 BYTE "X is lower",0dh,0ah,0
mssg2 BYTE "X is not lower",0dh,0ah,0

X = 10
Y = 12

.code

main PROC

compare:

mov eax, X
cmp eax, Y
jl xlower
mov eax, Y
cmp eax, X
jmp xhigher

xlower:
mov edx, OFFSET mssg1
Call WriteString
jmp compare

xhigher:
mov edx, OFFSET mssg2
Call WriteString


exit
main ENDP

END main

ragdog


mov eax,10
mov ebx,12
.if( eax < ebx )

      invoke printf,chr$ ("X is lower",CR,LF)
.else
       invoke printf,chr$ ("X is not lower",CR,LF)
.endif


or


mov eax,10
mov ebx,12
CMP EAX,EBX
JNB @notlower
PUSH  chr$ ("X is lower",CR,LF)
CALL printf
JMP @out
@notlower: 
PUSH chr$ ("X is not lower",CR,LF)
CALL printf
@out:

infoMASM

Thank you for your reply ragdog!! but I need to use calls such as WriteString but your code does help and I am working on it!

infoMASM

Thank you so much! I got it working the way I think it should!

;C code origional:
; double X
; double Y
; if(X < Y)
; printf("X is lower\n")
; else
; printf("X is not lower\n")
;

INCLUDE Irvine32.inc

.data
mssg1 BYTE "X is lower",0dh,0ah,0
mssg2 BYTE "X is not lower",0dh,0ah,0

X = 9
Y = 12

.code

main PROC

compare:

mov eax, X
mov ebx, Y
cmp eax, ebx
jnb xhigher
mov edx, OFFSET mssg1
call WriteString
jmp finished

xlower:
mov edx, OFFSET mssg1
Call WriteString
jmp finished

xhigher:
mov edx, OFFSET mssg2
Call WriteString

finished:


exit
main ENDP

END main

TWell

#4
more like from this.//C code original:
#include <stdio.h>
#define X  9
#define Y 12
int main(void) {
if(X < Y)
printf("X is lower\n");
else
printf("X is not lower\n");
  return 0;
}

C's double is REAL8 is asm.

That xlower block is useless ?

MichaelW

The compiler is not going to compare floating-point values (doubles in this case) with integer instructions (even though there is a way to do it). At least for the Microsoft Visual C++ Toolkit 2003 compiler, it will use FPU instructions (primarily fcomp), or SSE2 instructions (primarily comisd) with /arch:SSE2.
Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Quote from: infoMASM on March 14, 2014, 06:20:31 AM
double X;
double Y;
if( X < Y )
      printf("X is lower\n");
else
      printf("X is not lower\n")
In your C code, you would have to initialise x and y. If you want to stay close to the C version, and work with doubles, here is one option in Assembler (it needs the MasmBasic library):

include \masm32\MasmBasic\MasmBasic.inc

double equ <REAL8>  ; make it look like C ;-)
.data
x double      12.34
y double      56.78

  Init
  Fcmp x, y  ; more
  .if Sign?
      inkey "x is less than y"
  .elseif Zero?
      inkey "x and y are equal"
  .else
      inkey "x is greater than y"
  .endif
  Exit
end start

Note the real fun starts (in both C and Assembler) when x and y are almost equal - see this thread in the old Forum.

infoMASM

Thank you everyone for you input! Thank you for showing me how to use the MasmBasic library and getting it to work in that sense. I need to stick with the Irvine32.inc unfortunately but I will remember this for future reference.