EASy68K  
It is currently Sun Jan 26, 2020 11:24 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Loop that doesn't loop
PostPosted: Sun Sep 01, 2019 9:53 am 
Offline

Joined: Sun Sep 01, 2019 9:41 am
Posts: 3
I've set out a task for myself to write a program that asks the user to input a number and then prints is the number bigger or smaller than zero. If the user inputs 0, program terminates.

At the moment, program does what I intended to do, but only once. It doesn't loop.

Can anyone point out why is this happening?

This is the code I wrote:

Code:
ORG $1000   

   START:
   
   nextNumber:
        jsr printIntroText
        jsr numberIn
       
        move.l d1, d5           *D5 holds the entered number
       
        cmp #0, d5
        beq endProgram
       
        jsr newLine
        jsr printFirstPart
       
        move.l d5,d1
        jsr numberOut

        cmp #0, d5
        blt printIsSmaller
        bge printIsBigger

        jsr newLine

        bra nextNumber
       
   endProgram:
       
   SIMHALT
                       
   charOut:
        move.b #6, d0
        trap #15
        rts

   newLine:
        move.b #$0A, d1
        jsr charOut
        move.b #$0D, d1
        jsr charOut
        rts
   
   numberIn:
        move.b #4, d0
        trap #15
        rts
       
   numberOut:
        move.b #3, d0
        trap #15
        rts

**********STRING CONSTANTS       
numEnter:
        dc.l    'Enter number:',0
       
numberString:
        dc.l    'The number ',0
       
isBigger:
        dc.l    ' is bigger than zero.',0
       
isSmaller:
        dc.l    ' is smaller than zero.',0

**********PRINT STRINGS     
printIntroText:
        lea numEnter, a1
        move.b #14, d0
        trap #15
        rts
       
printFirstPart:
        lea numberString, a1
        move.b #14, d0
        trap #15
        rts
       
printIsSmaller:
        lea isSmaller, a1
        move.b #14, d0
        trap #15
        rts
       
printIsBigger:
        lea isBigger, a1
        move.b #14, d0
        trap #15
        rts   

   END    START       



Top
 Profile  
 
PostPosted: Mon Oct 07, 2019 11:33 am 
Offline

Joined: Fri Oct 04, 2019 10:52 am
Posts: 1
Is this supposed to loop indefinitely the way it was coded?


Top
 Profile  
 
PostPosted: Sun Oct 13, 2019 11:04 am 
Offline

Joined: Sun Sep 01, 2019 9:41 am
Posts: 3
Yes. User should keep entering the numbers, and if the input is zero, the program should stop looping and terminate. At least that was my thought process when writing it.


Top
 Profile  
 
PostPosted: Thu Oct 24, 2019 7:36 pm 
Offline

Joined: Thu Oct 24, 2019 11:34 am
Posts: 1
Got the mistake in it. When you jump (bge printIsBigger ) into the routine, afterwards you try to return from subroutine. But you never made a jump into a subroutine, and because of that the stack does not hold a return value, which breaks your code execution at this point, since you try to pop something from stack, that does not exist on the stack.


Top
 Profile  
 
PostPosted: Sun Nov 10, 2019 4:54 pm 
Offline

Joined: Sun Sep 01, 2019 9:41 am
Posts: 3
Thank you for the reply. I was off the forum for a quite some time and I saw your response just couple minutes ago.

I don't quite get what you meant with the call stack and return values, as it is the next chapter in the book I'm using. I understand the basic concept of it but not much. Would like you to elaborate it further if you want.

In the meantime, I made some changes to the original program, and it seems to be working as expected. Here's the v2:

Code:
START:
   
   lea numEnter, a1
   
   nextNumber:
        jsr printString
        jsr numberIn
       
        move.l d1,d5           *D5 holds the entered number
       
        cmp #0,d5
        beq endProgram
       
        lea numberString, a1
        jsr printString
       
        move.l d5,d1
        jsr numberOut

        cmp #0,d5
        blt printSmaller
       
        lea isBigger, a1
        jsr printString
        jmp continueLoop
       
    printSmaller:
        lea isSmaller, a1
        jsr printString
       
    continueLoop:
        jsr newLine
        lea numEnter, a1
               
        bra nextNumber
       
   endProgram:
   
   SIMHALT
                       
   charOut:
        move.b #6, d0
        trap #15
        rts

   newLine:
        move.b #$0A, d1
        jsr charOut
        move.b #$0D, d1
        jsr charOut
        rts
   
   numberIn:
        move.b #4, d0
        trap #15
        rts
       
   numberOut:
        move.b #3, d0
        trap #15
        rts

**********STRING CONSTANTS       
numEnter:
        dc.l    'Enter number:',0
       
numberString:
        dc.l    'The number ',0
       
isBigger:
        dc.l    ' is bigger than zero.',0
       
isSmaller:
        dc.l    ' is smaller than zero.',0

**********PRINT STRINGS     
printString:       
        move.b #14, d0
        trap #15
        rts       
**********PROGRAM END
   

   END    START


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group