Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   coldfire assembly - rts causes Illegal Instruction Exception (http://www.programmingforums.org/showthread.php?t=10405)

mika Jun 17th, 2006 9:50 PM

coldfire assembly - rts causes Illegal Instruction Exception
 
Hi all, I've been trying to fix an illegal exception for a couple of hours now with no success.

What happens is that I have a piece of code that checks for imputs and branches to appropriate subroutine. But after the subroutine finishes when executing the RTS to return to the called throws an Illegal exception. The address in PC seems to be 00000000 which is complete garbage. At the same time I did check my stack manipulation and it looks fine to me. The code snip:

Caller:

:

                          process_input
102000ac 2f08                          move.l A0, -(A7)
102000ae 2f00                          move.l D0, -(A7)
102000b0 4280                          clr D0
102000b2 41f9 1022 0000                lea BUFFER, A0
102000b8 1010                          move.b (A0), D0
102000ba 0c80 0000 0031                cmpi.l #$31, D0
102000c0 6700 0036                      beq  return_to_monitor
102000c4 0c80 0000 0032                cmpi.l #$32, D0
102000ca 6700 002e                      beq  calculator_mode
102000ce 0c80 0000 0033                cmpi.l #$33, D0
102000d4 6700 0026                      beq clock_mode
102000d8 0c80 0000 0034                cmpi.l #$34, D0
102000de 6700 002c                      beq clock_display
102000e2 201f                          move.l (A7)+, D0
102000e4 205f                          move.l (A7)+, A0
102000e6 4e75                          rts


Called sub:
                        clock_mode
102000fc 2f09                  move.l A1, -(A7)
102000fe 43f9 1021 0055        lea ERROR_OPTION_UNAVAILABLE, A1
10200104 6100 ff4c              bsr.w out_string
10200108 225f                  move.l (A7)+, A1
1020010a 4e75                  rts

This final instruction generates (snip of trace):
PC: 10200108 SR: 2704 [t.Sm.111...znZvc]
An: 1022000 00000000         
102000CA
An: 10220000 00000000 00000000 00000000 00000000 00000000 00000000 1023FFEC000000 00000000 00                                                       
Dn: 00000033 00000000 00000000 00000000 00000000 00000000 00000000 00000000 1020
An: 10220000 00000000 00000000 00000000 00000000 00000000 00000000 1023FFF0
Dn: 00000033 00000000 00000000 00000000 00000000 00000000 00000000 00000000

trace>


PC: 00000000  SR: A704 [T.Sm.111...xnZvc]
An: 10220000 00000000 00000000 00000000 00000000 00000000 00000000 1023FFF0
Dn: 00000033 00000000 00000000 00000000 00000000 00000000 00000000 00000000

Exception:
  Illegal Instruction
  format: 0x04  fault status: 0x0
  vector: 0x04  status reg:  0xA704


DaWei Jun 17th, 2006 10:41 PM

I'm not familiar with that particular assembler, but I doubt it differes from most in this respect: one CALLS a subroutine, then returns. Branching to a subroutine transfers control without recording a return address on the stack. The return instruction then just loads whatever junk was on the stack into the IP and off you go into the weeds, barfing all the way. On the other hand, when you call, the current IP is put onto the stack before the branch is made, and is there for the return instruction. Again, I don't know your particular assembler, but I'll shit in my flat hat if it's all that different.

mika Jun 17th, 2006 10:53 PM

Quote:

Originally Posted by DaWei
I'm not familiar with that particular assembler, but I doubt it differes from most in this respect: one CALLS a subroutine, then returns. Branching to a subroutine transfers control without recording a return address on the stack. The return instruction then just loads whatever junk was on the stack into the IP and off you go into the weeds, barfing all the way. On the other hand, when you call, the current IP is put onto the stack before the branch is made, and is there for the return instruction. Again, I don't know your particular assembler, but I'll shit in my flat hat if it's all that different.


This is not an issue in this case. When "branching" to a subroutine, (Beq in my case) coldfire saves PC on the stack, and then increments PC by the displacement needed to get to the branch location.

mika Jun 18th, 2006 1:51 PM

Quote:

Originally Posted by mika
This is not an issue in this case. When "branching" to a subroutine, (Beq in my case) coldfire saves PC on the stack, and then increments PC by the displacement needed to get to the branch location.


Actually my reply to you is complete garbage. You are absolutely right. When I read your comments late in the AM's I was quick to dismiss it because my brain was not functioning anymore. What I was using was indeed a conditional branch Bcc and what I actually wanted to do was branch to a subroutine. Which by backwards rationalization was what I did hence I replied by saying Pc is indeed saved on the stack. Ah, stories, a good night's sleep and I'm back in business. Thanks for replying.

DaWei Jun 18th, 2006 4:35 PM

You're entirely welcome. I couldn't pursue it in view of my non-familiarity with the particular assembler, it just seemed reasonable.


All times are GMT -5. The time now is 8:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC