PDA

View Full Version : Stack Overflow - Stack Underflow



massive attack
- 1st September 2010, 11:18
'================================================= ==============================
@ device INTRC_OSC_NOCLKOUT
@ device wdt_off
@ device pwrt_on
@ device protect_off
include "Tanimlar.inc"
'================================================= ==============================
Ana:
gosub Chk_KEYPAD
if say = 3 then
gosub kilit
endif
pause 1
goto Ana
'================================================= ==============================
Chk_KEYPAD:
for row = 0 to 3
PORTB = (dcd row) ^ $f
pause 1
col = PORTB >> 4
if (col <> $f) then 'tuşa basıldı
key = (row * 3) + (ncd (col ^ $f))
low buz
pause 70
high buz
while (PORTB >> 4) <> $f
pause 1
wend
if key = 11 then key = 0
TusBuff[tussay] = key
if psw.0 = 0 then
gosub Chk_tussay
else
gosub chk_tussay2
endif
key = 255
endif
next
return
'================================================= ==============================
Beep:
high ld1
low buz
pause 70
high buz
pause 70
low buz
pause 70
low ld1
high buz
return
'================================================= ==============================
Chk_SfrOK:
psw.1 = 0
psw.2 = 0
psw.3 = 0
psw.4 = 0
psw.5 = 0
psw.6 = 0
if (TusBuff[0] = SfrBuff[0]) and (TusBuff[1] = SfrBuff[1]) and (TusBuff[2] = SfrBuff[2]) and (TusBuff[3] = SfrBuff[3]) then psw.1 = 1
if (TusBuff[0] = SfrBuff[4]) and (TusBuff[1] = SfrBuff[5]) and (TusBuff[2] = SfrBuff[6]) and (TusBuff[3] = SfrBuff[7]) then psw.2 = 1
if (TusBuff[0] = SfrBuff[8]) and (TusBuff[1] = SfrBuff[9]) and (TusBuff[2] = SfrBuff[10]) and (TusBuff[3] = SfrBuff[11]) then psw.3 = 1
if (TusBuff[0] = SfrBuff[12]) and (TusBuff[1] = SfrBuff[13]) and (TusBuff[2] = SfrBuff[14]) and (TusBuff[3] = SfrBuff[15]) then psw.4 = 1
if (TusBuff[0] = SfrBuff[16]) and (TusBuff[1] = SfrBuff[17]) and (TusBuff[2] = SfrBuff[18]) and (TusBuff[3] = SfrBuff[19]) then psw.5 = 1
if (TusBuff[0] = MKSfrBuff[0]) and (TusBuff[1] = MKSfrBuff[1]) and (TusBuff[2] = MKSfrBuff[2]) and (TusBuff[3] = MKSfrBuff[3]) then psw.6 = 1
if psw.1 = 0 and psw.2 = 0 and psw.3 = 0 and psw.4 = 0 and psw.5 = 0 and psw.6 = 0 then
if TusBuff[tussay] = 12 or TusBuff[tussay] = 10 then
if psw.6 = 0 then
gosub Hata
endif
endif
endif
return
'================================================= ==============================
Chk_NewSfrOK:
psw.1 = 0
psw.2 = 0
psw.3 = 0
psw.4 = 0
psw.5 = 0
psw.6 = 0
if (TusBuff[5] = TusBuff[10]) and (TusBuff[6] = TusBuff[11]) and (TusBuff[7] = TusBuff[12]) and (TusBuff[8] = TusBuff[13]) then
psw.1 = 1
psw.6 = 1
endif
return
'================================================= ==============================
Sifre_Degis:
if psw.1 then
SfrBuff[0] = TusBuff[5]
SfrBuff[1] = TusBuff[6]
SfrBuff[2] = TusBuff[7]
SfrBuff[3] = TusBuff[8]
endif
if psw.2 then
SfrBuff[4] = TusBuff[5]
SfrBuff[5] = TusBuff[6]
SfrBuff[6] = TusBuff[7]
SfrBuff[7] = TusBuff[8]
endif
if psw.3 then
SfrBuff[8] = TusBuff[5]
SfrBuff[9] = TusBuff[6]
SfrBuff[10] = TusBuff[7]
SfrBuff[11] = TusBuff[8]
endif
if psw.4 then
SfrBuff[12] = TusBuff[5]
SfrBuff[13] = TusBuff[6]
SfrBuff[14] = TusBuff[7]
SfrBuff[15] = TusBuff[8]
endif
if psw.5 then
SfrBuff[16] = TusBuff[5]
SfrBuff[17] = TusBuff[6]
SfrBuff[18] = TusBuff[7]
SfrBuff[19] = TusBuff[8]
endif

Write 0, SfrBuff[0] 'Yeni şifre EEprom'un 0..3 adreslerine kaydedildi
Write 1, SfrBuff[1]
Write 2, SfrBuff[2]
Write 3, SfrBuff[3]
Write 4, SfrBuff[4] 'Yeni şifre EEprom'un 0..3 adreslerine kaydedildi
Write 5, SfrBuff[5]
Write 6, SfrBuff[6]
Write 7, SfrBuff[7]
Write 8, SfrBuff[8] 'Yeni şifre EEprom'un 0..3 adreslerine kaydedildi
Write 9, SfrBuff[9]
Write 10, SfrBuff[10]
Write 11, SfrBuff[11]
Write 12, SfrBuff[12] 'Yeni şifre EEprom'un 0..3 adreslerine kaydedildi
Write 13, SfrBuff[13]
Write 14, SfrBuff[14]
Write 15, SfrBuff[15]
Write 16, SfrBuff[16] 'Yeni şifre EEprom'un 0..3 adreslerine kaydedildi
Write 17, SfrBuff[17]
Write 18, SfrBuff[18]
Write 19, SfrBuff[19]
return
'================================================= ==============================
Chk_tussay:
select case tussay
case 4
gosub Chk_SfrOK
if psw.1 or psw.2 or psw.3 or psw.4 or psw.5 or psw.6 then
if TusBuff[tussay] = 12 then Kilit_acik
if TusBuff[tussay] <> 10 then Clr_TusBuff
gosub Beep
else
gosub Clr_TusBuff
endif
case 9
if TusBuff[tussay] <> 12 then Clr_TusBuff
gosub Beep
case 14
if psw.6 = 0 then
if TusBuff[tussay] <> 12 then Clr_TusBuff
gosub Beep
gosub Chk_NewSfrOK
if psw.1 or psw.6 then
gosub Beep
gosub Sifre_Degis
gosub Beep
gosub Clr_TusBuff
else
gosub Clr_TusBuff
endif
endif
case 16
if TusBuff[tussay] <> 12 then clr_TusBuff
gosub beep
gosub Chk_NewSfrOK
if psw.6 then
gosub Beep
gosub Sifre_Degis
gosub Beep
gosub Clr_TusBuff
else
gosub Clr_TusBuff
endif
case else
if TusBuff[tussay] = 12 then Clr_TusBuff
end select
tussay = tussay + 1
return
'================================================= ==============================
Chk_tussay2:
select case tussay
case 4
if psw.6 then
psw.0 = 0
if TusBuff[tussay] = 12 then Kilit_acik
if TusBuff[tussay] <> 10 then Clr_TusBuff
gosub Beep
else
gosub Clr_TusBuff
endif
case 9
if TusBuff[tussay] <> 12 then Clr_TusBuff
gosub Beep
case 14
if TusBuff[tussay] <> 12 then Clr_TusBuff
gosub Beep
gosub Chk_NewSfrOK
if psw.6 then
gosub Beep
gosub Sifre_Degis
psw.0 = 0
gosub Beep
gosub Clr_TusBuff
else
gosub Clr_TusBuff
endif
case else
if TusBuff[tussay] = 12 then Clr_TusBuff
end select
tussay = tussay + 1
return
'================================================= ==============================
Reset:
for cnt = 0 to 14
TusBuff[cnt] = cnt
next
low ld1
high buz
tussay = 0
psw.0 = 0
psw.1 = 0
psw.6 = 0
say = 0
return
'================================================= ==============================
Hata:
say = say + 1
high ld1
low buz
pause 200
low ld1
high buz
return
'================================================= ==============================
Kilit_acik:
if psw.0 = 0 then
say = 0
high mtr ' motoru çalıştır
High frw ' İleri dön
pause Frw_time ' ileri dönme süresi
low frw
low mtr
pause wait_time ' bekleme süresi
high mtr ' motoru çalıştır
high BCK ' geriye dön
pause bck_time ' geriye dönme süresi
low BCK
low mtr
endif
gosub Reset
return
'================================================= ==============================
Clr_TusBuff:
for cnt = 0 to 14
TusBuff[cnt] = cnt
next
tussay = 0
gosub Ana
'================================================= ==============================
Kilit :
psw.0 = 1
low buz
high ld1
gosub chk_KEYPAD
gosub Kilit
end
'================================================= ==============================

when simulating in proteus, errors occur:
Stack overflow executing CALL instruction;
the device is PIC16F628A
What's wrong?

Dave
- 1st September 2010, 11:44
massive attack , Where is the return and what are you trying to do here?

Kilit :
psw.0 = 1
low buz
high ld1
gosub chk_KEYPAD
gosub Kilit
end

This type of syntax will surely overflow the stack....

Dave Purola,
N8NTA

massive attack
- 1st September 2010, 13:30
Thank you for answer Dave,

i am trying to do a keypad controlled lock.

First i used the device pic16f84a and its memory is not enough to more password. Than i decided to use pic16f628a, the memory problem was solved. Although i used almost the same code in both microcontroller, now i have this type of problem.

mackrackit
- 1st September 2010, 13:33
What is in the include file?

massive attack
- 1st September 2010, 13:39
definitions



BCK_Time con 150 * 3 ' Motorun geri dönüş süresi
Wait_Time con 500 *3 ' Kilidin açık durumda bekleme süresi
FRW_Time con 155 * 3 ' Motorun ileri dönüş süresi
OPTION_REG.7 = 0 ' PORTB Pull-up aktif.
TrisA = 0 ' PORTA'nın bütün uçları çıkış
PortA = %00010000 ' Buzzer'ı sustur.
TrisB = %11110000 ' PORTB'nin 0-3 uçları çıkış, 4-7 uçları giriş
PortB = 0
FRW var PortA.3
MTR var PortA.1
LD1 var PortA.2
BCK var PortA.0
BUZ var PortA.4
cnt var byte
psw var byte
say var byte
key var byte
col var byte
row var byte
tussay var byte
TusBuff var byte[16]
SfrBuff var byte[24]
MKSfrBuff var byte[4]
EEPROM 0,[0,0,0,0] 'Başlangıç şifresi 1,1,1,1 EEprom'un 0..3
'adreslerine yazıldı
EEPROM 4,[1,1,1,1] 'Başlangıç şifresi 1,1,1,1 EEprom'un 0..3
'adreslerine yazıldı
EEPROM 8,[2,2,2,2] 'Başlangıç şifresi 1,1,1,1 EEprom'un 0..3
'adreslerine yazıldı
EEPROM 12,[3,3,3,3] 'Başlangıç şifresi 1,1,1,1 EEprom'un 0..3
'adreslerine yazıldı
EEPROM 16,[4,4,4,4] 'Başlangıç şifresi 1,1,1,1 EEprom'un 0..3
'adreslerine yazıldı
eeprom 20,[5,5,5,5] 'Master Key şifresi 9,8,7,6,5 EEprom'un 4..8
'adreslerine yazıldı
Read 0, SfrBuff[0] 'EEprom'un 0..3 adreslerinden şifreyi SfrBuff'a
Read 1, SfrBuff[1] 'kopyala
Read 2, SfrBuff[2]
Read 3, SfrBuff[3]
Read 4, SfrBuff[4] 'EEprom'un 0..3 adreslerinden şifreyi SfrBuff'a
Read 5, SfrBuff[5] 'kopyala
Read 6, SfrBuff[6]
Read 7, SfrBuff[7]
Read 8, SfrBuff[8] 'EEprom'un 0..3 adreslerinden şifreyi SfrBuff'a
Read 9, SfrBuff[9] 'kopyala
Read 10, SfrBuff[10]
Read 11, SfrBuff[11]
Read 12, SfrBuff[12] 'EEprom'un 0..3 adreslerinden şifreyi SfrBuff'a
Read 13, SfrBuff[13] 'kopyala
Read 14, SfrBuff[14]
Read 15, SfrBuff[15]
Read 16, SfrBuff[16] 'EEprom'un 0..3 adreslerinden şifreyi SfrBuff'a
Read 17, SfrBuff[17] 'kopyala
Read 18, SfrBuff[18]
Read 19, SfrBuff[19]
read 20, MKSfrBuff[0] 'EEprom'un 4..8 adreslerinden Master Key'i
read 21, MKSfrBuff[1] 'MKSfrBuff'a kopyala
read 22, MKSfrBuff[2]
read 23, MKSfrBuff[3]
tussay = 0 'Tuşa basılma sayısı
say = 0 'Yanlış şifrelerin sayısını saklar "3'e kadar."
psw = 0