Search code examples
assemblymasm

MASM Assembly jump instructions printing issue


   .386
   .MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD 
Include io.h
cr     equ 0DH
Lf     equ 0AH

       .STACK 4096
       .DATA

string byte 40 Dup (?)
number dword ?

runningSum dword 0

rejected byte " , Rejected", 0
positive byte cr, Lf, "Positive",0
negative byte cr, Lf, "Negative",0

numaschar byte 11 dup(?),0
newline byte cr,Lf,0

    .code
_start:

forever: input string, 40
         atod string
         mov number, eax
         cmp number,0
         jne processing
         je finish

processing:
    cmp number,10
        jg message

    cmp number,-10
        jl message

    cmp number,10
        jle not_rejected1

    cmp number,-10
        jge not_rejected2

    jmp jumpToTop

message: dtoa numaschar,number
         output numaschar
         output rejected
         output newline

not_Rejected1: cmp number, -10
                 jge processing2

not_Rejected2: cmp number,10
                 jle processing2

processing2:   cmp number, 0
                 jg addInstruct
                 jl subInstruct


addInstruct: add ebx, 1
             add edx,number
             jmp message2

subInstruct: add ecx, 1
             jmp message3

message2: dtoa numaschar, number
          output positive
          output newline

message3: dtoa numaschar, number
          output negative
          output newline


jumpToTop: jmp forever


finish:
    INVOKE ExitProcess, 0

PUBLIC _start
        END

When this program reads in a positive number, how come it prints both messages "positive" and "negative" but I need it to print only the message "positive"

Any help is appreciated. Thanks !

.....................................................................................................


Solution

  • Have a look at the code that follows the label message2 - You dont have a jmp instruction in there. As a result, after printing "positive" the code 'falls-through' to the next case and then prints "negative".

    I guess you intended to jump either to forever or to finish.