'--------------------------------------------------------------
'BASCOM-AVR IDE Version : 1.11.7.7
'Compiler: Version 1.11.7.7
'Serial : Serial DEMO
'
'Author: Luciano
'Processor: AT90S2313 / Select Chip = 2313def.dat from the IDE
'Code Purpose: Showing a stack problem. No other purpose.
'Forum Post: "Error in Frame Space (Stack problem)".
'Where to run the test: BASCOM AVR Simulator or AVRStudio Simulator.
'
'Calculations based on the information found in the Help file.
''(See help file topic "Options Compiler Chip").
'
'When you run the code with the simulator, open the
'memory window from the address $00DF downward to the address $0060.
'The memory window will display the HW stack,
'SW stack, Frame space and global variable space. (The whole SRAM).
''(My comments are based on the AT90S2313 data address space).
'--------------------------------------------------------------
'
'The directives $hwstack, $swstack and $framesize override the
'values from the IDE Options/Compiler/Chip.
'
$hwstack = 32
$swstack = 10 'see comments in the Sub
$framesize = 41 'see comments in the Sub
Declare Sub My_sub(my_parameter As String)
Dim My_string_global As String * 10
Dim My_byte_global As Byte
My_string_global = "ABCDEFGHIL" 'stored from 0060 to 006A 11 bytes (10+1)
My_byte_global = 255 'stored at address 006B 1 byte
''(The address 0060 will be visible in the
''simulator memory window at the beginning of the Soft stack).
Call My_sub(my_string_global)
End
Sub My_sub(my_parameter As String) 'the parameter is passed by reference
Local First_number As Integer 'will use 2 bytes of Frame
Local Second_number As Integer 'will use 2 bytes of Frame
Local Addition_result As Integer 'will use 2 bytes of Frame
Local My_string_local As String * 10 'will use 11 bytes of Frame (10+1)
'FRAME SIZE REQUIREMENTS for this test=33 bytes
'Bascom AVR internal conversion routines need a maximum of 16 bytes.
'Therefore the minimal needed frame size for this test should be:
'2+2+2+11+16=33 bytes ($framesize=33)
'SOFT STACK SIZE REQUIREMENTS for this test=10 bytes
'Each local variable uses 2 bytes: We have 4 local variables so we need 8 bytes.
'Each parameter that is passed to a sub uses 2 bytes: so we need 2 more bytes.
'Therefore the minimal needed Soft Stack size for this test should be:
'2+2+2+2+2=10 bytes ($swstack=10).
First_number = 10011 'HEX 271B
Second_number = 20022 'HEX 4E36
Addition_result = 13330 'HEX 3412 just to see where written in the mem.window
My_string_local = "1234567890"
Addition_result = First_number + Second_number
Print "Result=" ; Addition_result ; " (Result should be 30033)"
''''''P r i n t with internal conversion.
'The Print below will only print the string >1234567890< when the
'$framesize is set bigger than 40.
Print ">" ; My_string_local ; "<"
''''''P r i n t without internal conversion.
End Sub
'=================================================================
'
' THE PROBLEM
'
' The Frame start address for the Bascom AVR internal conversion
' routines is correctly set at the address 8D. (After the first
' Print you will see that data output of the internal routines is
' written in the frame space starting at the address 8D).
' What is wrong is the address (location) where the local variables are
' stored in the frame space. The first byte used to store local data is
' at the address A5.(The simulator shows that in the mem.window).
' The first byte used to store locals should be at address 9D, not at A5.
' This because only 16 bytes before location 9D should be reserved by
' the compiler for the internal conversion routines.In this example
' the reserved 16 bytes are at the following addresses:
' 8D,8E,8F,90,91,92,93,94,95,96,97,98,99,9A,9B,9C where the byte
' at address 8D is the first byte of the frame space.If you run this
' example you will see that the compiler is reserving 24 bytes instead
' of 16 bytes for the internal conversion routines.
'
' If I follow the instructions in the Bascom AVR Help file, the
' above code should run with a Frame Size set to 33 bytes.
' (See comments in the above Sub).If you try a Frame Size of 33 bytes
' you will see that the data of the local variables will override the
' SW stack where the addresses are stored.If you try this example, start
' with a Frame Size = 41. (With 41 the code will run fine). If you set
' the Frame Size to 40, you will see that the address of the local string
' is overridden and therefore the second Print will print nothing.If you
' set the Frame Size to 33 you will see that all the addresses of the local
' variables are overridden, so that the first Print will print 0 and the
' second Print will print nothing.
'
' In the Bascom AVR report, the value Framestart also gives an indication
' of the problem.The values Space left and S-Stackstart are wrong in the
' report.(During simulation the S-Stackstart address is OK).
' (See the end of this text for part of the report with added comments).
'
' With the Frame Size set to 41 there is a frame overflow signaled by
' the Bascom AVR simulator with the checkbox "Frame overflow".
' I don't know why we get this warnig because the 10 bytes of the
' SW stack are not overridden by the data of the local variable.
' (With the Frame Size set to 41 the code runs fine).
'
'==================================================================
' Data memory space of the above example.
'
' ($hwstack = 32, $swstack = 10, $framesize = 41).
'
' I have calculated all the values and compared them with the values
' displayed in the simulator memory window of Bascom AVR and AVRstudio.
'
'+ --------------+----------------------------------------------
'| HW stack | (Hardware stack grows downward)
'| First byte=DF |
'| Last byte=C0 |
'| Size HEX =20 | $hwstack = 32
'+ --------------+-----------------------------------------------
'| SW stack | (Software stack grows downward)
'| First byte=BF |
'| Last byte=B6 |
'| Size HEX =0A | $swstack = 10
'+ --------------+-----------------------------------------------
'| Frame | (Frame space grows upward)
'| First byte=8D |
'| Last byte=B5 |
'| Size HEX =29 | $framesize = 41
'+ --------------+-----------------------------------------------
'| Global | My_string_global uses = 11 bytes (10 + null)
'| First byte=60 | My_byte_global uses = 1 byte
'| Last byte=8C | Free space left = 33 bytes
'| Size HEX =2D | -----------
'| | Total space for global = 45 bytes
'+ --------------+-----------------------------------------------
' TOTAL SRAM = 32+10+41+45 = 128 bytes
'
'=================================================================
'
' THE BASCOM AVR Report
'
'Report : STACKTEST
'Date : 03-05-2005
'Time : 16:25:04
'
'Compiler : BASCOM-AVR LIBRARY V 1.11.7.7, DEMO Edition
'Processor : 90S2313
'SRAM : 80 hex
'EEPROM : 80 hex
'ROMSIZE : 800 hex
'
'ROMIMAGE : 258 hex -> Will fit into ROM
'ROMIMAGE : 600 dec
'FLASH USED : 29 %
'BAUD : 9600 Baud
'XTAL : 4000000 Hz
'BAUD error : 0.16%
'
'Stack start : DF hex (OK, same as my value, same in the sim.mem.window).
'Stack size : 20 hex (OK, same as my value, same in the sim.mem.window).
'S-Stacksize : A hex (OK, same as my value, same in the sim.mem.window).
'S-Stackstart : C0 hex (Should be BF hex, in the sim.mem.window it is at BF).
'Framesize : 29 hex (OK, same as my value, same in the sim.mem.window).
'Framestart : 96 hex (Should be 8D hex, read the problem description).
'Space left : 34 dec (Should be 33,if you add 34 to the rest you get 129)
'
'LCD DB7 : PORTB.7
'LCD DB6 : PORTB.6
'LCD DB5 : PORTB.5
'LCD DB4 : PORTB.4
'LCD E : PORTB.3
'LCD RS : PORTB.2
'LCD mode : 4 bit
'
'-------------------------------------------------------------------
'Variable Type Address(hex) Address(dec)
'-------------------------------------------------------------------
'HWSTACK Word 005D 93
'SWSTACK Word 001C 28
'FRAME Word 0004 4
'COUNTER1 Word 004C 76
'TIMER1 Word 004C 76
'CAPTURE1 Word 0044 68
'COMPARE1A Word 004A 74
'PWM1A Word 004A 74
'ERR Bit 0006 6
'MY_STRING_GLOBAL String (OK) 0060 (OK) 96 (OK)
'MY_BYTE_GLOBAL Byte (OK) 006B (OK 107 (OK)
'
'------------------------------------------------------------------
|