EASy68K  
It is currently Fri Apr 10, 2020 2:47 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: HELP PLEASE
PostPosted: Fri Nov 09, 2012 12:06 pm 
Offline

Joined: Thu Nov 08, 2012 6:07 pm
Posts: 12
Hi all, can anyone tell me who to solve this:
In the main program:

 Read the ASCII number string “0123456789” from define address

 Pass it to SUB1 as a define address and size of string

 Display the new string on terminal

 In the SUB1:
 Convert the ASCII number to binary number

 Pass two consecutive numbers in string to SUB2 as a format of stack

 Convert the addition result from SUB2 to ASCII

 Instantiate the addition result to the define string address

 In SUB2:
 add two consecutive number
 Do the same operation for all number in string.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2012 3:10 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1118
I'm sure there are many people on this forum that could solve this problem. None of them will however, because we do not do student homework for them. That would defeat its purpose. Attempt the solution on your own and ask us specific questions about your program and we will help.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 10, 2012 5:03 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1118
This simplest way to pass values to and from a subroutine is in registers. Using registers requires coordination between the calling code and the subroutine as to which registers to use.

Using the stack to pass values is a bit more involved but does not require tracking register usage between the calling code and the subroutine.

Here is an example program that demonstrates calling a subroutine using both parameter passing techniques.

Code:
*-----------------------------------------------------------
* Program Number: Tutorial 4 for EASy68k
* Written by    : Chuck Kelly
* Date Created  : Nov-10-2012
* Description   : This program prompts for and gets 2 numbers from the keyboard.
*                 A subroutine is called to add the 2 numbers and return the sum.
*                 Adder1 subroutine uses registers to pass the parameters.
*                 Adder2 subroutine uses the stack to pass the parameters.
* This program is public domain.
*-----------------------------------------------------------

START   ORG $1000

    * Input 2 numbers and add them with Adder1 subroutine.
    * The parameters and return value are passed using registers
    move    #14,D0      task 14, display string
    lea     prompt,A1   address of string to display in A1
    trap    #15         trap #15 performs task

    move    #4,D0       task 4, input number into D1.L
    trap    #15         perform task
    move.l  D1,D2       D2 = num1

    move    #14,D0      task 14, display string
    lea     prompt,A1   address of string to display in A1
    trap    #15         trap #15 performs task

    move    #4,D0       task 4, input number into D1.L
    trap    #15         perform task, D1 = num2

    bsr     Adder1      D1 = D1 + D2

    move    #14,D0      task 14, display string
    lea     result,A1   address of string to display in A1
    trap    #15         trap #15 performs task
    move    #3,D0       task 3, display number in D1.L
    trap    #15         trap #15 performs task

    bsr     newLine




    * Input 2 numbers and add them with Adder2 subroutine.
    * The parameters and return value are passed on the stack
    move    #14,D0      task 14, display string
    lea     prompt,A1   address of string to display in A1
    trap    #15         trap #15 performs task

    move    #4,D0       task 4, input number into D1.L
    trap    #15         perform task
    move.l  D1,-(SP)    push num1

    move    #14,D0      task 14, display string
    lea     prompt,A1   address of string to display in A1
    trap    #15         trap #15 performs task

    move    #4,D0       task 4, input number into D1.L
    trap    #15         perform task
    move.l  D1,-(SP)    push num2
    move.l  #0,-(SP)    make room for sum on the stack
   
    bsr     Adder2      sum = num1 + num2
    move.l  (SP)+,D1    pull sum from stack, D1 = sum
    lea     (8,SP),SP   clean up the stack

    move    #14,D0      task 14, display string
    lea     result,A1   address of string to display in A1
    trap    #15         trap #15 performs task

    move    #3,D0       task 3, display number in D1.L
    trap    #15         trap #15 performs task

    bsr     newLine
* Stop execution
    stop    #$2000


*------------------------------------
* Add the number in register D1 to the number in register D2.
* The sum is returned in register D1.
* D1 = D1 + D2
Adder1
    add.l   d2,d1
    rts                         return

*------------------------------------
* Add two numbers passed as parameters on the stack.
* The sum is returned on the stack.
* The OFFSET directive is used to access the parameters by name.
* 4 bytes for A0 saved by link, 4 bytes for saving D0
        offset  2*4
sum     ds.l    1
num2    ds.l    1
num1    ds.l    1
        org     *           end the offset

Adder2
    link    A0,#0           create stack frame
    move.l  D0,-(SP)        save D0
    move.l  (num1,A0),D0    access num1 parameter
    add.l   (num2,A0),D0    add num2 parameter
    move.l  D0,(sum,A0)     store result in sum parameter
    move.l  (SP)+,D0        restore D0
    unlk    A0
    rts                     return

*------------------------------------
* Subroutine to display Carriage Return and Line Feed
newLine
    movem.l d0/a1,-(SP)         push d0 & a1
    move    #14,d0              task number into D0
    lea     crlf,a1             address of string
    trap    #15                 display return, linefeed
    movem.l (SP)+,d0/a1         restore d0 & a1
    rts                         return

*------------------------------------
* Variable storage and strings
* ds (define storage) is used to reserve memory for variables
* dc (define constant) is used to place data in memory
prompt  dc.b    'Enter number: ',0  null terminated string
result  dc.b    'The sum is: ',0
crlf    dc.b    $d,$a,0         carriage return & line feed, null


        END     START           end of program with start address specified

_________________
Prof. Kelly


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 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:  
Powered by phpBB® Forum Software © phpBB Group