Author Topic: Strange File Size  (Read 7375 times)

Ampersand Laboratories Team

  • Guest
Strange File Size
« on: November 26, 2006, 05:48:10 PM »
Hello. I issued the command

nasm -f bin -o a_very_simple_os.bin a_very_simple_os.asm

to create an OS with the following Assembly code:

======================================
; A Very Simple Kernel For A Very Simple OS!
; By Pietro Gagliardi

%DEFINE NL 13, 10

[ORG 0x7C00]
[BITS 32]

SEGMENT .DATA
   COLOR DW 0x0F
   MESG1 DB "Welcome to ", 0
   MESG2 DB "A ", 0
   MESG3 DB "Very ", 0
   MESG4 DB "Simple ", 0
   MESG5 DB "Operating ", 0
   MESG6 DB "System", 0
   MESG7 DB "!", NL, 0
   MESG8 DB "This is a very simple OS!", NL, 0
   MESG9 DB "Hit Ctrl+Alt+Del to restart the computer.", NL, 0
   XPOS DB 0
   YPOS DB 0

SEGMENT .TEXT
   GLOBAL makevid
   GLOBAL main
   GLOBAL halt
   GLOBAL printmsg1
   GLOBAL printmsg2
   GLOBAL printmsg3
   GLOBAL printmsg4
   GLOBAL printmsg5
   GLOBAL printmsg6
   GLOBAL printmsg7
   GLOBAL printmsg8
   GLOBAL printmsg9
   GLOBAL println
   GLOBAL setcolor
   GLOBAL dochar
   GLOBAL cprint
   GLOBAL sprint

JMP main ; BEGIN THE OS!

halt:
   JMP halt

setcolor:
   MOV AH, [COLOR]
   RET

main:
   CALL makevid
   CALL setcolor
   CALL printmsg1
   MOV WORD [COLOR], 0x0B
   CALL printmsg2
   MOV WORD [COLOR], 0x5E
   CALL printmsg3
   MOV WORD [COLOR], 0x19
   CALL printmsg4
   MOV WORD [COLOR]. 0x6F
   CALL printmsg5
   MOV WORD [COLOR], 0xF0
   CALL printmsg6
   MOV WORD [COLOR], 0x5C
   CALL printmsg7
   MOV WORD [COLOR], 0x0F
   CALL printmsg8
   CALL printmsg9
   JMP halt

makevid:
   XOR AX, AX
   MOV DS, AX
   MOV SS, AX
   MOV SP, 0x9C00
   MOV AX, 0xB800
   MOV ES, AX
   RET

printmsg1:
   MOV SI, MESG1
   CALL sprint
   RET

printmsg2:
   MOV SI, MESG2
   CALL sprint
   RET

printmsg3:
   MOV SI, MESG3
   CALL sprint
   RET

printmsg4:
   MOV SI, MESG4
   CALL sprint
   RET

printmsg5:
   MOV SI, MESG5
   CALL sprint
   RET

printmsg6:
   MOV SI, MESG6
   CALL sprint
   RET

printmsg7:
   MOV SI, MESG7
   CALL sprint
   RET

printmsg8:
   MOV SI, MESG8
   CALL sprint
   RET

printmsg9:
   MOV SI, MESG9
   CALL sprint
   RET

sprint:
   LODSB
   CMP AL, 0
   JNE dochar
   ADD BYTE [YPOS], 1
   MOV BYTE [XPOS], 0
   RET

dochar:
   CALL cprint

cprint:
   MOV CX, AX
   MOVZX AX, BYTE [YPOS]
   MOV DX, 160
   MUL DX
   MOVZX BX, BYTE [XPOS]
   SHL BX, 1
   MOV DI, 0
   ADD DI, AX
   ADD DI, BX
   MOV AX, CX
   STOSW
   ADD BYTE [XPOS], 1
   RET

TIMES 510-($-$$) DB 0
DB 0x55
DB 0xAA
======================================

For some reason, the .bin file comes out 620K. Without the TIMES statement, it is less than 300K. I need it to be exactly 512K. What am I doing wrong?

nobody

  • Guest
Re: Strange File Size
« Reply #1 on: November 27, 2006, 03:00:39 AM »
:) Fooled by Nasm's "fake" segments! What Nasm does with "segments", in "-f bin" mode, is to put the ".data" segment after the ".text" segment (and ".bss" after that, if you had one). So the "times" line actually comes in the middle of the file, not at the end where you want it. I'd eliminate the "section .data" entirely, and put everything in "section .text" (Nasm's default, if you don't specify a "segment/section). If you want a "section/segment .data", put the "times" line at the end of that.

What are the "global" directives supposed to be doing? That's an instruction to Nasm to inform the linker about those symbols, and since there's no linker involved... I *expected* Nasm to complain about that (in "-f bin" mode), but I was wrong - apparently they're silently ignored. In any case, they're not doing you any good...

Happy bootin',
Frank

Ampersand Laboratories Team

  • Guest
Re: Strange File Size
« Reply #2 on: November 28, 2006, 01:06:13 AM »
Thank you. This helped me a lot!