0

This program take a user input and determines if the value is negative or positive. I want to use a for loop so that a user can check multiple numbers, in this case I set the loop at three inputs. What I am having difficulty with is allowing the user to enter '0' to exit. JS does the same thing as JG & JL, i just commented them out to consolidate the two tests into one jump. When I add a conditional statement to check for zero I get a "jump" error of at least 5 bytes. Please help. I have commented out the faulty code. Thanks.

.586
.MODEL FLAT

INCLUDE io.h

.STACK 4096

.DATA
titleLbl BYTE   "Is your number positive or negative ", 0
formula BYTE "It's anyones guess, well no not really, cuz conditional statements.....", 0
prompt BYTE    "Please enter a number: (enter '0' to exit)", 0
string  BYTE    40 DUP (?)
resultLbl BYTE  "The number you entered is:", 0
positiveLbl BYTE    "Positive", 0
negativeLbl BYTE    "Negative", 0
zeroLbl BYTE    "You Entered '0'", 0
zero_messageLbl BYTE    "Exiting", 0

.CODE

_MainProc PROC
output titleLbl, formula

mov ecx, 3                  ;the for loop will run 3 times
forCount: input   prompt, string, 40        ; prompt user for a number
    atod    string                  ;convert to integer
    cmp eax, 0                      ;compare user input to value stored (0)

    js negative
    ;jg positive        ;jump conditional if input is greater than 0
    ;jl negative        ;jump conditional if input is less than 0
    ;jz zero            ;jump conditional if input is equal to 0

    positive:   ;condition if greater than; ouput
        output  resultLbl, positiveLbl  ; output the even message
            jmp exit                    

    negative:   ;condition if less than; ouput
        output  resultLbl, negativeLbl  ; output the odd message
            jmp exit

    ;zero:      ;condition if equal to; ouput
        ;output zeroLbl, zero_messageLbl
            ;jmp exit

    exit:   ;quit jump              ;end if/else/if conditional
loop forCount
quit:

    mov eax, 0          ;clear memory   
    ret
_MainProc ENDP
END
Chris Snook
  • 19
  • 1
  • 6
  • you can do another conditional jump in the fall-through path of the first one. `jl negative` / `jz zero` should work fine, because `jl` isn't taken if `eax=0`. Or if you insist on using [the slow `loop` instruction](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently), put the check for zero at the bottom of your loop and use `loopnz`, then `jl` at the top of the loop (with flags still set from the `cmp / loopnz`). For the first iteration, you can jump into the loop at the cmp/loopnz so you still exit on 0. – Peter Cordes Oct 25 '17 at 06:11
  • 1
    Copy/paste the error message that says "jump error". Possibly there's a symbol called `zero` already, but otherwise IDK why you'd have a problem with `jz zero` uncommented. – Peter Cordes Oct 25 '17 at 06:16
  • 1
    Error A2075 jump destination too far : by 7 byte(s) – Chris Snook Oct 25 '17 at 15:06
  • this is with jz uncommented along with the conditional jump exit uncommented – Chris Snook Oct 25 '17 at 15:06
  • "use loopnz, then jl at the top of the loop (with flags still set from the cmp / loopnz)" – Chris Snook Oct 25 '17 at 15:08
  • I'm not sure I follow this, what would this look like? I wasn't aware you could do that. – Chris Snook Oct 25 '17 at 15:08
  • IDK why your assembler isn't using a rel32 jump instead of a rel8 jump; you used `.586`, so it should know that it's allowed to use instructions that 8086 don't support. Maybe try `jz NEAR zero`. – Peter Cordes Oct 25 '17 at 21:51

1 Answers1

0

Because the problem, as said Chris Snook, is the too far length of the jump, you can write:

cmp eax, 0                      ;compare user input to value stored (0)

jle negative_or_zero

;positive:  ;here there is always a positive number; ouput
    output  resultLbl, positiveLbl  ; output the even message
        jmp exit                    

negative_or_zero:
jz zeroz                        ;jump conditional if input is equal to 0

negative:   ;condition if less than; ouput
    output  resultLbl, negativeLbl  ; output the odd message
        jmp exit

zeroz:      ;condition if equal to; ouput
    ;output zeroLbl, zero_messageLbl
        ;jmp exit
Paolo Fassin
  • 361
  • 2
  • 11