Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2008, 08:17 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 234 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 9 h 25 m 35 sec
Reputation Power: 0
Send a message via MSN to NetBSD
The Motorola HC11F1

Maybe anyone with more assembly knowledge can answer this:

Code:
          org	        $100
Loop	nop
          jsr 	         Loop


Is this a valid subroutine definition and jump to subroutine?

With jsr I'm using the stack and using something like "bra Loop" (branch always) I'm not?

Thanks

Reply With Quote
  #2  
Old June 30th, 2008, 12:23 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,215 Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 10 h 4 m 17 sec
Reputation Power: 574
I suppose it depends on what you call 'valid'. While it will assemble and run, it is, a written, an infinite loop... except that it will crash when the stack overflows.

I gather that the real question is, can an instruction in a subroutine call that subroutine, or, more broadly, can assembly language subroutines be recursive. Changing how you word a question often gives you the answer, doesn't it?

To dig a little deeper: in assembly language, there isn't really such a thing as a subroutine, except as a set of conventions in the minds of the coders. So long as the instruction and operands are valid, and the syntax correct, the assembler will assemble the instruction, and once assembled, it is nothing more than a bit pattern for the operation. The CPU itself has no concept of labels; those are a convenience provided by the assembler. If the operation is a valid one, the CPU will happily perform it, regardless of how much or little sense it makes. Even the assembler does only the most minimal sanity checking, at least in most assemblers.

Thus, it is up to the assembly programmer to make sure that an operation makes sense. In the case of a jump to subroutine, it is indeed a combination of a stack push and a jump (note that AFAICT this is an absolute jump, not a relative branch). What gets pushed is the address following the JSR instruction, which the RTS instruction then uses to return from.

Now, there are two things to keep in mind here: one, the RTS operation will try to use whatever is on the stack as the return address, regardless of what is actually there. Again, the CPU doesn't know anything from one instruction to another, save through the state of memory and the registers; the whole point of using the stack is to give it that trail of breadcrumbs, as it were, and if you push anything else after the call without popping it off before it reaches the RTS, it will try jumping to wherever that appears to point to.

The second thing to keep in mind is that it pushes an address every time you call a routine, and each JSR needs to eventually be matched to an RTS. If you get into a recursive loop that doesn't eventually bottom out (that is, there are no cases where it returns), it will quickly work its way through the whole stack space. Since the 68HC11 has no memory protection AFAIK, this means that it will eventually trash the program itself. On processors with protected memory, it will cause a stack fault when it tries to push to a location outside of the allocated stack space, a somewhat less catastrophic result but one which still crashes the program. This means that, like with any other kind of loop, you need to have someplace in it that eventually checks for the end condition.

HTH, C&CW.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : June 30th, 2008 at 12:35 PM.

Reply With Quote
  #3  
Old June 30th, 2008, 01:07 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 234 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 9 h 25 m 35 sec
Reputation Power: 0
Send a message via MSN to NetBSD
Thanks for this

Reply With Quote
  #4  
Old June 30th, 2008, 02:28 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 234 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 9 h 25 m 35 sec
Reputation Power: 0
Send a message via MSN to NetBSD
I realize I'm still confused about something here:

How do I define subroutines so they are 'outside' of normal program flow, i.e.

Code:
		org 		$100
subr1 	nop
		nop
		rts
		ldaa 		#$FF
		jsr	 	subr1
		staa 		$1000


what happens when I run this program and it hits rts (return from subroutine) before I call it later on?

Does the subroutine have to exist at the first spot in the program it is used or is there a better way to do this?

Reply With Quote
  #5  
Old July 1st, 2008, 04:52 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,215 Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level)Schol-R-LEA User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 10 h 4 m 17 sec
Reputation Power: 574
The subroutine can and in most cases should be defined after the end of the main program that calls it. Also, most assemblers - though not usually those for simulators - can assemble programs to object code which can be linked to other assembled modules when creating the executable.
Code:
        org        $100

        ldaa        #$FF
        jsr        subr1
        staa        $1000
;; put whatever operation is used to end the program here

;; subroutines
subr1   nop
        nop
        rts
If you prefer to have the subroutines first, you may (depending on the assembler) need to have a jump to the main routine before the subroutines.
Code:
        org        $100

        jmp        main
subr1   nop
        nop
        rts

main
        ldaa       #$FF
        jsr        subr1
        staa       $1000


Just what you'll need to do will depend on the assembler or simulator, of course.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > The Motorola HC11F1


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT