|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 Short • Understanding the C/C++ Preprocessor Taming Python • A 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. |
|
#3
|
|||
|
|||
|
Thanks for this
|
|
#4
|
|||
|
|||
|
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? |
|
#5
|
||||
|
||||
|
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
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > The Motorola HC11F1 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|