OSDev.org

The Place to Start for Operating System Developers
It is currently Wed May 01, 2024 7:00 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: Getting The Amount Of RAM
PostPosted: Sat May 12, 2007 5:12 am 
Offline
Member
Member

Joined: Tue Nov 14, 2006 11:59 am
Posts: 174
Hi again

I seem to run into a new problem daily... Anyhow I'm trying to get the amount of RAM available.

...EDIT (see later posts)...

Sorry for asking sooo many questions recently :?.
Lster


Last edited by Lprogster on Sun May 13, 2007 4:05 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 7:01 am 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 9:29 pm
Posts: 2426
Location: Canada
Could you perhaps tell a little more about your OS?

Are you in protected mode? If you are you can't use BIOS interrupts..

_________________
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 7:19 am 
Offline
Member
Member

Joined: Tue Nov 14, 2006 11:59 am
Posts: 174
...

Thanks,
Lster


Last edited by Lprogster on Tue Oct 23, 2007 11:18 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 7:21 am 
Offline
Member
Member

Joined: Sun Jul 30, 2006 8:16 am
Posts: 149
Location: The Netherlands
Brynet-Inc wrote:
Are you in protected mode? If you are you can't use BIOS interrupts..


Since he states he isn't using GRUB (and he didn't mention any other ready-made bootloader), presumably he's writing his own and can use the BIOS from there, before entering PM (assuming he enters it at all).
Of course, that's still a requirement he must take care to follow: only use the BIOS interrupts if in real mode (or VM86).
Parts of BIOS memory that can be used without calling interrupts (video memory at 0xb8000 in text mode, the VBE3 protected mode interface, etc.) are fine even in protected mode, of course...


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 8:10 am 
Offline
Member
Member
User avatar

Joined: Fri Dec 15, 2006 5:26 pm
Posts: 437
Location: Church Stretton Uk
Shouldn't int 15 be int 0x15?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 8:32 am 
Offline
Member
Member

Joined: Sun Jul 30, 2006 8:16 am
Posts: 149
Location: The Netherlands
mathematician wrote:
Shouldn't int 15 be int 0x15?

Yes. Yes it should. Good catch :). (Just tested his code with that modification)


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 9:10 am 
Offline
Member
Member

Joined: Tue Nov 14, 2006 11:59 am
Posts: 174
Even changing that doesn't help. Any ideas?

Thanks,
Lster


Last edited by Lprogster on Sun May 13, 2007 4:06 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 10:32 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

Lprogster wrote:
I can't seem to use EAX, EDX, EBX and ECX in 16 bit mode.


Are you sure about this?

You should be able to use 32-bit registers for 80386 and later CPUs, and I don't think Qemu can emulate 80286 or older CPUs.

I'd be tempted to think there's other problems, like something corrupting/overwriting your code, something corrupting/overwriting data used by the BIOS (IVT, BDA, EBDA), using a bad location for your stack, forgetting to use "bits 16", not loading the entire thing from disk, bugs in (your version of) Qemu, accidentally booting an old version of your code and wondering why it behaves the same, etc.

Single stepping through the code to see exactly what is happening would help too - I'm not sure if Qemu supports this or not. Trying your code on Bochs might be a good idea anyway (if it has the same problem on Bochs, then you could be reasonably sure it's not caused by bugs in your version of Qemu).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 10:33 am 
Offline
Member
Member

Joined: Sun Jul 30, 2006 8:16 am
Posts: 149
Location: The Netherlands
Do you have "bits 16" at the top of your code?

Maybe an example will help.
The following works for me: (bs.asm)
Code:
org 0x7c00
bits 16

main:
   ; clear screen
   mov ax, 0xb800
   mov es, ax
   mov fs, ax   ; needed later
   xor di, di
   mov ax, 0x0720
   mov cx, 25*80
   rep stosw
   
   xor bp, bp   ; needed later as well
   
   ; set up stack and ds, es
   mov ss, bp
   mov ds, bp
   mov es, bp
   mov sp, 0x7c00
   
   ; Initial value for interrupt
   xor ebx, ebx
   
.iter:
   ; Preserved:
   ; ebx -- continuation
   ; fs:ebp -- screen position
   ; ss:esp -- stack
   
   ; Clobbered:
   ; eax
   ; es
   ; ds
   ; edx
   ; ecx
   ; esi
   ; edi
   
   mov di, 0x500
   mov eax, 0x0000E820
   mov edx, 0x534D4150
   
   mov ecx, 20
   
   int 0x15
   
   jc .halt
   
   mov si, di
   
   lodsd
   mov [low], eax
   lodsd
   mov [high], eax
   call printhex
   mov eax, [low]
   call printhex
   
   mov ax, '-'
   call printchar
   
   lodsd
   add [low], eax
   lodsd
   adc [high], eax
   mov eax, [high]
   call printhex
   mov eax, [low]
   call printhex
   
   mov ax, ':'
   call printchar
   
   lodsd
   call printhex
   
   call newline
   
   cmp ebx, 0
   je .ebxzero
   
   jmp .iter

.ebxzero:
   mov word [fs:24*80*2+1], 'Z'
.halt:
   mov word [fs:24*80*2], 'D' | 0x0700
   cli
   hlt
   jmp .halt


   ; Requires:
   ; eax    -- number to print
   ; fs:ebp -- preserved
   
   ; Clobbers:
   ; ecx
   ; edx
   ; eax
printhex:
   mov edx, eax
   mov cx, 8
.loop:
   push cx
   ; prepare shift
   dec cx
   shl cx, 2
   
   ; apply shift
   mov eax, edx
   shr eax, cl
   
   pop cx
   
   ; mask one digit
   and eax, 0xf
   
   ; print digit
   mov ax, [hexdigs+eax]   ;; reads one byte too much
   mov ah, 0      ;; clear extra byte
   call printchar
   
   loop .loop
   
   ret


   ; Requires:
   ; fs:ebp -- Next character on screen
   ; al    -- character to print
   
   ; Clobbers:
   ; ah
printchar:
   mov ah, 0x07
   mov word [fs:bp], ax
   add bp, 2
   ret

newline:
   mov bp, [line]
   add bp, 80 * 2
   mov [line], bp
   cmp bp, 24*80*2
   jae main.halt
   ret

low:   dd 0         ; The low 32 bits of a 64-bit address
high:   dd 0         ; The high 32 bits of a 64-bit address
hexdigs:db "0123456789abcdef"   ; Obvious :)
line:   dw 0         ; The location of the next line

   times 510-($ - $$) db 0

   db 0x55, 0xAA


Assemble with
Code:
nasm -f bin -o bs.bin bs.asm
(or nasmw if you're on Windows) and run with
Code:
qemu -fda bs.bin

It should print the memory map to the screen (in hexadecimal). On my QEMU, the result is
Code:
0000000000000000-000000000009fc00:00000001
0000000000100000-0000000008000000:00000001
(modulo typos)

I'm using QEMU 0.8.2. It also works in my Bochs (2.3).


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 1:33 pm 
Offline
Member
Member

Joined: Tue Nov 14, 2006 11:59 am
Posts: 174
...

Thanks guys...
Lster


Last edited by Lprogster on Tue Oct 23, 2007 11:19 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 2:14 pm 
Offline
User avatar

Joined: Wed Oct 18, 2006 10:06 am
Posts: 3
Location: Ireland
The reason the code is failing is because int 13h ah=0 (Reset drive) expects the drive number in DL (also int 0x13 ah=2 expects the drive number in dl and head in dh) You are getting stuck in an infinite loop constantly resetting the drive.

You are thrashing EDX with the E820 magic number. The BIOS sets dl to the ID of the boot drive before when loading your boots sector. If you want to get the memory map before reading from disk, you should save the value of DL before thrashing it.

Tip: when debugging these sort of things it helps to poke chars to the screen in various places so that you can see the exact flow of the code.

HTH

Shecks


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 12, 2007 4:14 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 9:29 pm
Posts: 2426
Location: Canada
The attached code prints the '!' in the top corner...

Hope this helps.. :)


Attachments:
File comment: Fixed code..
boot.asm [1.72 KiB]
Downloaded 31 times

_________________
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Top
 Profile  
 
 Post subject:
PostPosted: Sun May 13, 2007 4:10 am 
Offline
Member
Member

Joined: Tue Nov 14, 2006 11:59 am
Posts: 174
Life is good... again :D!

Thanks everyone soooooo much,
Lster


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 13, 2007 7:55 pm 
Offline
Member
Member
User avatar

Joined: Sun Feb 18, 2007 7:28 pm
Posts: 1564
Rather then starting a new thread on the same subject...

How would one find out the amount of RAM available in pmode?
Just curious...


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 13, 2007 8:27 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 9:29 pm
Posts: 2426
Location: Canada
neon wrote:
Rather then starting a new thread on the same subject...

How would one find out the amount of RAM available in pmode?
Just curious...

That's a tough one.. I'm guessing the best way would be to do it before switching into protected mode, storing the value some how? :?

If you use grub, You can find out the amount of ram using multiboot_info structure..

_________________
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group