what does [BITS 32] do in nasm?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MarkZar
Posts: 13
Joined: Thu Feb 07, 2013 11:53 pm

what does [BITS 32] do in nasm?

Post by MarkZar »

Hi :D
I'm new to os development, and now some small questions confused me.

According to the book I am reading, when you are writing codes in protected mode using NASM, you should add [BITS 32] in place, just as follows:

Code: Select all

[SECTION .s32]
ALIGN	32
[BITS 32]

LABEL_PM_START:
	mov ax, SelectorVideo        ;GDT Selector pointed to the video descriptor
	mov gs, ax

	mov ah, 0Fh	
	mov al, 'P'
	mov [gs:((80 * 0 +39) * 2)], ax        ;display 'P'
	jmp $
I'm really confused with that [BITS 32], because I dont know what intheworld NASM does when I add it. I compiled the asm file and then disassembled it using ndisasm, founding the right codes generated as follows:

Code: Select all

00090320  66B81B00          mov ax,0x1b
00090324  8EE8              mov gs,ax
00090326  B40F              mov ah,0xf
00090328  B050              mov al,0x50
0009032A  6566A34E000000    mov [gs:0x4e],ax
00090331  EBFE              jmp short 0x90331



Exactly same with what I write in asm file, but once I changed the [BITS 32] to [BITS 16], I found the disassembled codes is whole different:

Code: Select all

00090320  66B81B008EE8      mov eax,0xe88e001b
00090326  B40F              mov ah,0xf
00090328  B050              mov al,0x50
0009032A  6566A34E00        mov [gs:0x4e],eax
0009032F  0000              add [bx+si],al
00090331  EBFE              jmp short 0x331

Why?
Last edited by MarkZar on Fri Feb 08, 2013 6:37 am, edited 5 times in total.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: what does [BITS 32] do in nasm?

Post by bluemoon »

MarkZar wrote:I'm really confused with that [BITS 32], because I dont know what intheworld NASM does when I add it.
Let me google that for you
It's well documented on the manual. However if you are having problem on the bit concept for different architectures, it's a long story.

By the way I think this should go to General Programming section.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: what does [BITS 32] do in nasm?

Post by Gigasoft »

[Bits 32] generates code that is suitable for a 32 bit code segment. Specifically, the default operand and address sizes are 32 bit and the meaning of 66h/67h prefixes are reversed.
MarkZar
Posts: 13
Joined: Thu Feb 07, 2013 11:53 pm

Re: what does [BITS 32] do in nasm?

Post by MarkZar »

bluemoon wrote:
MarkZar wrote:I'm really confused with that [BITS 32], because I dont know what intheworld NASM does when I add it.
Let me google that for you
It's well documented on the manual. However if you are having problem on the bit concept for different architectures, it's a long story.

By the way I think this should go to General Programming section.
Thank you, in fact I have looked through the manual, but still a little confused with it.Maybe practice is more than any words :D
MarkZar
Posts: 13
Joined: Thu Feb 07, 2013 11:53 pm

Re: what does [BITS 32] do in nasm?

Post by MarkZar »

Gigasoft wrote:[Bits 32] generates code that is suitable for a 32 bit code segment. Specifically, the default operand and address sizes are 32 bit and the meaning of 66h/67h prefixes are reversed.
Thank you :D
Post Reply