Weird Multitasking Errors
Re:Weird Multitasking Errors
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
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
Oops! They were suposed to be pops. It was a copying error.
K.J.
Hey, that's okay. I never knew that. Now I do since you mentioned it.Sorry I went on about esp in the stack. I totally forgot that the value of esp popped off with popa gets discarded.
Agreed. I'm scouring the web ATM looking for other people's multitasking code to see where mine differs.I still think it's a stack problem though.
No, there are 8. for(i=0; i<7; i++) will loop 8 times.Plus you're only creating 7 gp registers right now
K.J.
Re:Weird Multitasking Errors
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
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
Variable i goes from 0-6, so that's 7 times.No, there are 8. for(i=0; i<7; i++) will loop 8 times.
Re:Weird Multitasking Errors
first time through: i is set to 0, it's not ++Variable i goes from 0-6, so that's 7 times.
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
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
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
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.K.J. wrote:first time through: i is set to 0, it's not ++Variable i goes from 0-6, so that's 7 times.
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
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
Code: Select all
00000017 837DFC06 cmp dword [ebp-0x4],byte +0x6
0000001B 7E02 jng 0x1f
0000001D EB07 jmp short 0x26
Code: Select all
0000001F 8D45FC lea eax,[ebp-0x4]
00000022 FF00 inc dword [eax]
00000024 EBF1 jmp short 0x17
The last bit of code is
Code: Select all
00000026 B800000000 mov eax,0x0
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;
Code: Select all
MOV EAX, 0
ret
Re:Weird Multitasking Errors
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
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
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.
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.