Page 2 of 2
Re:Weird Multitasking Errors
Posted: Sat Nov 09, 2002 3:54 am
by Curufir
KJ:
You're pushing the gp registers twice, I'm assuming those pushes before the iret are supposed to be pops. The pusha/popa wasn't the problem though.
Sorry I went on about esp in the stack. I totally forgot that the value of esp popped off with popa gets discarded.
I still think it's a stack problem though. Plus you're only creating 7 gp registers right now, when there are 8 popped off, not that that should affect anything, I think stack[7] will just be filled with whatever happened to be there in memory.
Curufir
Re:Weird Multitasking Errors
Posted: Sat Nov 09, 2002 6:37 pm
by K.J.
Oops! They were suposed to be pops. It was a copying error.
Sorry I went on about esp in the stack. I totally forgot that the value of esp popped off with popa gets discarded.
Hey, that's okay. I never knew that. Now I do since you mentioned it.
I still think it's a stack problem though.
Agreed. I'm scouring the web ATM looking for other people's multitasking code to see where mine differs.
Plus you're only creating 7 gp registers right now
No, there are 8. for(i=0; i<7; i++) will loop 8 times.
K.J.
Re:Weird Multitasking Errors
Posted: Sat Nov 09, 2002 8:34 pm
by .bdjames
Old, Ugly, Buggy. When I was just playing around. Not
sure if this is the working version.
align 4
tasks:
times MAX_TASKS dd 0
current: dd 0
nTasks: dd 0
TaskSwitch: pushad
mov eax, [current]
mov ebx, [nTasks]
mov [4*eax-4+tasks], esp
dec eax
jnz NextTask
mov eax, ebx
NextTask: mov esp, [4*eax-4+tasks]
mov [current], eax
mov eax, 0x0C
out 0x70, al
in al, 0x71
mov eax, 0x20
out 0xA0, al
out 0x20, al
popad
iret
AddTask:
%define pointer esp+4+32
%define stack esp+8+32
%define value esp+12+32
cli
pushad
mov eax, [nTasks]
cmp eax, MAX_TASKS
jb Continue2
popad
xor eax, eax
sti
ret 12
Continue2: mov ecx, [pointer]
mov ebx, [stack]
mov edx, [value]
mov ebp, esp
mov esp, ebx
sub esp, 44
inc eax
mov [4*eax-4+tasks], esp
mov [esp+32], ecx
mov [nTasks], eax
mov [esp+28], edx
mov dword [esp+36], CODE_SEGMENT
mov dword [esp+40], 1000000010b
dec eax
jz FirstTask
dec eax
jz FirstSwitch
mov esp, ebp
popad
sti
ret 12
FirstSwitch: push TaskSwitch
call EnableTimer
mov esp, ebp
popad
sti
ret 12
FirstTask: mov dword [current], 1
popad
iret
EnableTimer:
%define pointer2 esp+4
mov ecx, [pointer2]
mov eax, [pointer2]
shr ecx, 16
mov [IDT+IRQ8<<3], ax
mov [6+IDT+IRQ8<<3], cx
in al, 0x21
and eax, 0xFB
out 0x21, al
in al, 0xA1
and eax, 0xFE
out 0xA1, al
mov eax, 0x0A
out 0x70, al
in al, 0x71
and eax, 0xF0
or eax, 6
out 0x71, al
mov eax, 0x0B
out 0x70, al
in al, 0x71
or eax, 0x40
out 0x71, al
ret 4
Re:Weird Multitasking Errors
Posted: Sun Nov 10, 2002 9:57 am
by Will
No, there are 8. for(i=0; i<7; i++) will loop 8 times.
Variable i goes from 0-6, so that's 7 times.
Re:Weird Multitasking Errors
Posted: Sun Nov 10, 2002 6:17 pm
by K.J.
Variable i goes from 0-6, so that's 7 times.
first time through: i is set to 0, it's not ++
i=0, is i<7? yes i++
i=1, is i<7? yes i++
i=2, is i<7? yes i++
i=3, is i<7? yes i++
i=4, is i<7? yes i++
i=5, is i<7? yes i++
i=6, is i<7? yes i++
i=7, is i<7? no. end loop
So... it loops through 8 times.
K.J.
Re:Weird Multitasking Errors
Posted: Sun Nov 10, 2002 9:17 pm
by .bdjames
int main(){
int i;
for(i = 0;i < 7;i++);
return 0;
}
00000000 55 push ebp
00000001 89E5 mov ebp,esp
00000003 83EC08 sub esp,byte +0x8
00000006 83E4F0 and esp,byte -0x10
00000009 B800000000 mov eax,0x0
0000000E 29C4 sub esp,eax
00000010 C745FC00000000 mov dword [ebp-0x4],0x0
00000017 837DFC06 cmp dword [ebp-0x4],byte +0x6
0000001B 7E02 jng 0x1f
0000001D EB07 jmp short 0x26
0000001F 8D45FC lea eax,[ebp-0x4]
00000022 FF00 inc dword [eax]
00000024 EBF1 jmp short 0x17
00000026 B800000000 mov eax,0x0
0000002B C9 leave
0000002C C3 ret
0000002D 90 nop
0000002E 90 nop
0000002F 90 nop
Re:Weird Multitasking Errors
Posted: Mon Nov 11, 2002 12:46 am
by Schol-R-LEA
K.J. wrote:
Variable i goes from 0-6, so that's 7 times.
first time through: i is set to 0, it's not ++
i=0, is i<7? yes i++
i=1, is i<7? yes i++
i=2, is i<7? yes i++
i=3, is i<7? yes i++
i=4, is i<7? yes i++
i=5, is i<7? yes i++
i=6, is i<7? yes i++
i=7, is i<7? no. end loop
I'm afraid you are mistaken. It tests the loop condition 8 times, but since the condition is tested before the iteration (unlike the increment, which follows after the loop iteration), it skips the loop code on the eighth test.
Looking at the assembly listing in .bdjames' posting, it becomes clearer. Looking at the relevant fragment, you will see that
Code: Select all
00000010 C745FC00000000 mov dword [ebp-0x4],0x0
sets the local test variable [tt]i[/tt] to 0 before the loop begins, then
Code: Select all
00000017 837DFC06 cmp dword [ebp-0x4],byte +0x6
0000001B 7E02 jng 0x1f
0000001D EB07 jmp short 0x26
performs the comparison, then either jumps to the loop code, or else past it. (the actual comparison is somewhat confusing, as it actually is testing "[tt]i[/tt] is not greater than 6", and jumping to the loop code on true).
Code: Select all
0000001F 8D45FC lea eax,[ebp-0x4]
00000022 FF00 inc dword [eax]
00000024 EBF1 jmp short 0x17
This code would normally go after the loop body, but in this case there is no body to speak of. Take note that the loop itself is an unconditional jump; breaking out of it is done before the loop body.
The last bit of code is
which is where the break out jump (the unconditional jump following the test but before the loop body) goes to. All it is doing is loading the return value into EAX.
To put it another way, if you were to take this generated assembly code and rewrite as C code, it would come out as:
Code: Select all
i = 0;
loop:
if (!(i > 6))
goto lp_body;
else
goto lp_end;
lp_body:
i++;
goto loop;
lp_end:
return 0;
I can only assume that he had optimization turned off. By all rights, dead code elimination should have replaced the entire main() function with
Re:Weird Multitasking Errors
Posted: Mon Nov 11, 2002 2:21 am
by .bdjames
int main(){
int i;
for(i = 0;i < 7;i++) printf("%i\n",i);
return 0;
}
E:\DOCUME~1\ADMINI~1\Desktop\NEWFOL~2>a
0
1
2
3
4
5
6
Re:Weird Multitasking Errors
Posted: Mon Nov 11, 2002 2:53 am
by K.J.
I guess there is still some stuff 'bout C that I don't know.
Thanks for pointing that out. It didn't fix the multitasking error, but it did help me fix some errors with memory management.
K.J.