EASy68K  
It is currently Sun Sep 15, 2019 10:40 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue May 24, 2011 7:27 pm 
Offline

Joined: Mon May 23, 2011 9:59 am
Posts: 3
Hi im new here and there's this small error with my code.

to summarize the whole thing it basically allows you to input '1's and '0's only while it counts the number of '1's , '0's and the total input count.

1)its almost done but how do i get rid of that weird symbol at the beginning of every input??

2)and also, after each input the '1's and '0's will be stored starting from address $2000, eg: input = 101, so $2000 = 1 , $2001 = 0 , $2002=1.
I cant seem to clear the addresses when the program loops without messing up the loop itself

other than the 2 minor problems stated above, code works perfectly fine

here's the current working code

Code:
START:   ORG   $1000      ; first instruction of program

   move.l    #$7ffe,a7    ; initialise stack pointer
   move.b   #0,d1      ; clear d1 for safety

   move.b   #12,d0      ; turn off keyboard echo
   trap   #15      
   move.l   #reserve,a2   ; save memory location of length to a2


*-----------input message--------
       
begin   move.l   #inmess,a0
   move.b   #14,d0
   trap    #15
   
   
*------------input-----------   


loop   move.l   #0,a0   ; clears a0 which stores inmess
   move.b   #5,d0   
   trap   #15   ;initiates trap task 5, reads a character from keyboard into d1.l   

   cmp.b   #'1',d1   
   beq   store1   
   cmp.b   #'0',d1
   beq   store0   
   cmp.b   #cr,d1   ;compares d1 to 'enter' aka CR
   beq   end1   ;end1 begins printing the result

   bra   invalid
   

*-----------error input message--------
       
invalid   lea   error,a1 ;using a1 to store strings from now on   
   move.b   #14,d0
   trap    #15   

   clr.l   d1   ;clears d1 again to accept new input again

   bra   loop
   
   
*-----------storing 0s----------
   
store0   move.b    d1,(a2)+   ;a2 is the reserve, stores the '0's into reserve $2000+
   add.b   #1,d2      ;d2 is the number of '0's         

   move.b   #6,d0      
   trap   #15      
   bra   loop      
   
   

*------------storing 1s--------

store1   move.b   d1,(a2)+   ;same as above, but stores the '1's instead
   add.b   #1,d3      ;d3 is used to store the number of '1's

   move.b   #6,d0
   trap   #15
   bra   loop
   


*-----------print line1 = 'there are (no. of zero)'----------

end1   move.l   #0,a1      ;clear a1 so next string can be stored
   move.l   #line1,a1   ;store line1 into a1

   move.b   #14,d0   
   trap    #15

   clr.l   d1      ;clears d1 for safety
   move.b   d2,d1      ;moves the total no. of '0's into d1 to be read
   move.b   #3,d0      
   trap   #15

   bra   end2
   
*-----------print line2 = 'zero and +(no. of 1)'-----------

end2   move.l   #0,a1      ;again, clearing for safety
   move.l   #line2,a1   

   move.b   #14,d0
   trap   #15

   clr.l   d1      ;clears d1 to make way for d3
   move.b   d3,d1      ;d3 which is the total no. of '1's is moved to d1
   move.b   #3,d0
   trap   #15
   
   bra   end3
   
*---------print line3 = '+ one in the input etc'--------

end3   move.l   #0,a1      
   move.l   #line3,a1   
   
   move.b   #14,d0      
   trap   #15
   
   move.b   d2,d4      ;moving d2 to d4, d4 acts as the total of '1's and '0's
   add.b   d3,d4      ;by adding d3 to d4, d4 now houses the total count of '1's and '0's
   clr.l   d1      ;clear d1 for safety
   move.b   d4,d1      ;move d4 to d1 so trap can display value
   
   move.b   #3,d0
   trap   #15
   
   bra   again
   
*----------continuation prompt---------

again   move.l   #0,a1      ;clearing to move msg 'cont' into a1
   move.l   #cont,a1   ;continuation message
   
   move.b   #14,d0
   trap   #15

   clr.l   d1      ;clearing all the previously used registers to prepare for the next loop
   clr.l   d2
   clr.l   d3
   clr.l   d4
   move.l   #0,a0
   move.l   #0,a1

   
   move.b   #5,d0
   trap   #15

   cmp.b   #'Y',d1
   beq   begin      ;loops to beginning again
   cmp.b   #'Q',d1
   beq   close      ;ends program
   
   bra    again

*---------end program-------------      
      
close   MOVE.B   #9,D0
   TRAP   #15      ; halt simulator

* Variables and Strings
   ORG   $2000
reserve ds.b   200
cr   equ   $0d
lf   equ   $0a

   ORG   $3000
inmess   dc.b   'Input 1 or 0',cr,lf,0
   

error   dc.b   cr,lf,'only 1 and 0 accepted. Please try again',cr,lf,0

line1   dc.b   cr,lf,'There are ',0

line2   dc.b   ' "zero" and ',0

line3   dc.b   ' "one" in the input and total input string is ',0

cont   dc.b   cr,lf,'Do you wish to continue? (Y = yes / Q = Quit)',cr,lf,0


   END   START      ; last line of source


_________________
ball ball ball baaaaaall sack!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2011 2:40 am 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
There is a lot of excessive micro management of the registers, this has less to do with safety, and more about knowing how the registered are altered by the operations being performed.

Let's use 32-bit registers, fix the register usage, not jump to the next instruction, and generally cleanse the unnecessary junk.
Code:
cr      equ     $0d
lf      equ     $0a

START   ORG     $1000      ; first instruction of program

        move.l  #$7ffe,a7    ; initialise stack pointer

        move.b  #0,d1       ; clear d1 for safety??
        move.b  #12,d0      ; turn off keyboard echo
        trap    #15

*-----------input message--------

begin   clr.l   d2      ; zero count
        clr.l   d3      ; one count

        move.l  #reserve,a2   ; save memory location of length to a2

        move.l  #inmess,a1
        move.b  #14,d0   ; out string
        trap    #15

*------------input-----------

loop    move.b  #5,d0   ; read character
        trap    #15

        cmp.b   #'1',d1
        beq     store1

        cmp.b   #'0',d1
        beq     store0

        cmp.b   #cr,d1   ;compares d1 to 'enter' aka CR
        beq     end1   ;end1 begins printing the result

*-----------error input message--------

invalid lea     error,a1 ;using a1 to store strings from now on
        move.b  #14,d0
        trap    #15
        bra     loop

*-----------storing 0s----------

store0  move.b  d1,(a2)+   ;a2 is the reserve, stores the '0's into reserve $2000+
        addq.l  #1,d2      ;d2 is the number of '0's

        move.b  #6,d0      ; out char
        trap    #15
        bra     loop

*------------storing 1s--------

store1  move.b  d1,(a2)+   ;same as above, but stores the '1's instead
        addq.l  #1,d3      ;d3 is used to store the number of '1's

        move.b  #6,d0      ; out char
        trap    #15
        bra     loop

*-----------print line1 = 'there are (no. of zero)'----------

end1    move.l  #line1,a1  ;store line1 into a1
        move.b  #14,d0     ;out string
        trap    #15

        move.l  d2,d1      ;moves the total no. of '0's into d1 to be read
        move.b  #3,d0      ;out decimal
        trap    #15

*-----------print line2 = 'zero and +(no. of 1)'-----------

        move.l  #line2,a1
        move.b  #14,d0     ;out string
        trap    #15

        move.l  d3,d1      ;d3 which is the total no. of '1's is moved to d1
        move.b  #3,d0      ; out decimal
        trap    #15

*---------print line3 = '+ one in the input etc'--------

        move.l  #line3,a1
        move.b  #14,d0     ; out string
        trap    #15

        move.l  d2,d1      ;moving d2 to d1, d1 acts as the total of '1's and '0's
        add.l   d3,d1      ;by adding d3 to d1, d1 now houses the total count of '1's and '0's

        move.b  #3,d0      ; out decimal
        trap    #15

*----------continuation prompt---------

again   move.l  #cont,a1   ;continuation message
        move.b  #14,d0     ; out string
        trap    #15

        move.b  #5,d0      ; input char
        trap    #15

        cmp.b   #'Y',d1
        beq     begin      ; loops to beginning again

        cmp.b   #'y',d1
        beq     begin      ; loops to beginning again

        cmp.b   #'Q',d1
        beq     close      ; ends program

        cmp.b   #'q',d1
        beq     close      ; ends program

        bra     again

*---------end program-------------

close   MOVE.B  #9,D0
        TRAP    #15      ; halt simulator

* Variables and Strings
        ORG     $2000

reserve ds.b    200

        ORG     $3000

inmess  dc.b    'Input 1 or 0',cr,lf,0

error   dc.b    cr,lf,'only 1 and 0 accepted. Please try again',cr,lf,0

line1   dc.b    cr,lf,'There are ',0

line2   dc.b    ' "zero" and ',0

line3   dc.b    ' "one" in the input and total input string is ',0

cont    dc.b    cr,lf,'Do you wish to continue? (Y = yes / Q = Quit)',cr,lf,0

        END     START      ; last line of source


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2011 4:20 am 
Offline

Joined: Mon May 23, 2011 9:59 am
Posts: 3
thanks so much for your help clive :)

i just assumed i should keep clearing registers to avoid confusion later.

were the problems solved by removing my unnessecary clearing of registers?
and how did u clear the stored numbers in $2000 each time the program loops??

_________________
ball ball ball baaaaaall sack!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2011 4:40 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
I cleaned it up so I could understand the flow.

The initial message didn't come up because you used the wrong register.

It goes back to $2000 now because I pulled some of the initialization inside the "begin" loop, so each time you take new input it resets the counts, and memory address.

Be cognoscente to the flow of the code to understand where the original failed. Compare and contrast my fixed example, with your original, observe where the code falls between the branch/loop targets.

Single stepping should also be instructive.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2011 6:41 pm 
Offline

Joined: Mon May 23, 2011 9:59 am
Posts: 3
oops made a silly blunder for the message string...
i will do a comparison between the 2 codes, thanks again clive!

_________________
ball ball ball baaaaaall sack!


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: Bing [Bot] 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:  
Powered by phpBB® Forum Software © phpBB Group