lori bailey
lori bailey

Reputation: 31

Assembly - Compare instruction not working

I am at the end of writing an assembly program and the trouble area is my compare instruction, it doesn't seem to be working and nothing I do seems to make it work so ALL input being read in is being sent to rejected

code:

loop_top:         
     input  string,40                  ; read ASCII characters
     atod  string                      ; convert to integer
     mov  number1, eax                 ; store in memory
     jz done

     cmp number1, 50                   ; number1>50?
     jg rejected
     cmp number1, -50                  ; number1<-50?
     jl rejected

rejected:
     dtoa number1ch,eax                ; convert to ASCII characters
     output number1ch                  ; print value and message to screen
     output reject 
     jmp loop_top

     mov eax, number1                  ; store in register
     cmp eax,0                         ; eax > 0?
     jnl isPositive

Problem area ? :

         cmp number1, 50                  ; number1>50?
         jg rejected
         cmp number1, -50                 ; number1<-50?
         jl rejected

rejected:
         dtoa number1ch,eax               ; convert to ASCII characters
         output number1ch                 ; print value and message to screen
         output reject 
         jmp loop_top

So any number ex: 4, 50, 51, -3, -50, -51 .. are all being sent to rejected. Can someone explain to me why? Thanks!

Upvotes: 0

Views: 1828

Answers (2)

Magoo
Magoo

Reputation: 80023

         cmp number1, 50                  ; number1>50?
         jg rejected
         cmp number1, -50                 ; number1<-50?
         jl rejected

rejected:

Well?

If the number is greater than 50, it goes to rejected because of the jg
If the number is less than -50, it goes to rejected because of the jl
And if neither of these jumps takes place, execution flows through to the next instruction which is (you fill in) r_j_ct_d

Upvotes: 1

Paul R
Paul R

Reputation: 212969

It looks like you're just "falling through" to rejected even when number1 is valid:

     cmp number1, 50                   ; number1>50?
     jg rejected                       ; <<< jump to rejected if number1 > 50
     cmp number1, -50                  ; number1<-50?
     jl rejected                       ; <<< jump to rejected if number1 < -50

     ;; we get here if -50 <= number1 <= 50, i.e. if 
     ;; number1 is in the valid range...

     ;; but then we fall through to `rejected` anyway...

     ;; so code for processing `number1` needs to go here
     ;; (or you could just put a `jmp` here to the
     ;; relevant code)...

rejected:

Upvotes: 1

Related Questions