LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2010, 06:36 PM   #1
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Rep: Reputation: 15
Masm to Nasm


Can anyone help me convert the following code from MASM to NASM

Code:
.486p
_CODE	SEGMENT  use16 para PUBLIC 'CODE'

DESCRIPTOR	MACRO	name, limit, base, flags
name	label	dword
	dw	limit and 0FFFFh
	dw	base and 0FFFFh
	db	(base and 0FF0000h) shr 16
	db	flags and 0FFh
	db	((flags and 0F00h) shr 4) or ((limit and 00F0000h) shr 16)
	db	(base and 0FF000000h) shr 24

	ENDM

;The GDT for protected mode operation.

align	4
GDTstart label	qword
DESCRIPTOR	nulldesc,	   0, 0,    0h	; null descriptor
DESCRIPTOR	datadesc,    0FFFFFh, 0,  092h	; data segment
DESCRIPTOR	stackdesc,    0FFFFh, 0,  092h	; stack segment
DESCRIPTOR	fatdatadesc, 0FFFFFh, 0, 0892h	; BIG data segment
DESCRIPTOR	fatcodedesc, 0FFFFFh, 0, 0C9Eh	; BIG code segment
DESCRIPTOR	codedesc,     0FFFFh, 0,  09Eh	; code segment
DESCRIPTOR	realdatadesc, 0FFFFh, 0,  093h	; real mode data segment
GDTend	label	qword

;In-memory GDT pointer for the lgdt call.

align	4
GDTptr		label	fword
gdtlimit	dw	GDTend - GDTstart	;Size of the GDT.
gdtbase 	dd	0			;Linear address of GDT.

save_ds dw	?
save_es dw	?
save_fs dw	?
save_gs dw	?
save_ss dw	?

A20_State db	?			; 0 = A20 is already un-wrapped
					; 1 = We need to remove A20 wrap

_CODE	ends
_CODE	segment
 
Old 07-02-2010, 08:18 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

It actually looks syntactically correct as-is?

Q: What specific errors did you encounter when you tried to assemble it with nasm?

It also looks like it might be a good way to trash (deliberately or otherwise) your OS's memory mapping.

Q: What exactly are you trying to do with this code, anyway?
 
Old 07-03-2010, 12:35 AM   #3
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Yes it is totally correct for MASM.
I have not tried much to code it up in nasm because I am not much familiar with equivalent syntax for Nasm. I know its kinda basic but I didn't find much straight forward thing.
I am suppose to write up assembly code for Linux that will switch between protection mode and real mode to trigger some SMI interrupts.
 
Old 07-06-2010, 07:08 PM   #4
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
i have figured out most of the code but still i cant seem to figure out one thing .. can anyone please help me with the following:
Code:
DESCRIPTOR    MACRO    name, limit, base, flags
name    label    dword
    dw    limit and 0FFFFh
    dw    base and 0FFFFh
    db    (base and 0FF0000h) shr 16
    db    flags and 0FFh
    db    ((flags and 0F00h) shr 4) or ((limit and 00F0000h) shr 16)
    db    (base and 0FF000000h) shr 24
    ENDM
 
;The GDT for protected mode operation.
    align    4
GDTstart label    qword
DESCRIPTOR    nulldesc,       0, 0,    0h    ; null descriptor
DESCRIPTOR    datadesc,    0FFFFFh, 0,  092h    ; data segment
DESCRIPTOR    stackdesc,    0FFFFh, 0,  092h    ; stack segment
DESCRIPTOR    fatdatadesc, 0FFFFFh, 0, 0892h    ; BIG data segment
DESCRIPTOR    fatcodedesc, 0FFFFFh, 0, 0C9Eh    ; BIG code segment
DESCRIPTOR    codedesc,     0FFFFh, 0,  09Eh    ; code segment
DESCRIPTOR    realdatadesc, 0FFFFh, 0,  093h    ; real mode data segment
GDTend    label    qword
I dont understand "DESCRIPTOR nulldesc, 0, 0, 0h" and other lines like that ..
 
Old 07-07-2010, 02:04 AM   #5
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
OK, first of all I don't understand MASM directives, but what I think those "DESCRIPTOR" lines mean are - they're setting aside space in memory for the Global Descriptor Table, which is needed for Protected Mode operation. Please do a Google to find out exactly what it is, or search on the alt.lang.asm and alt.os.assembly newsgroups.
You can't ENTER Protected Mode when you're within Linux, as Linux already does that as part of its bootup - it's a Protected Mode OS, and you DEFINITELY can't enter Real Mode when you're within Linux.
What are SMI Interrupts, btw?
 
Old 07-07-2010, 11:28 AM   #6
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by resetreset View Post
You can't ENTER Protected Mode when you're within Linux, as Linux already does that as part of its bootup - it's a Protected Mode OS,
I would be calling an assembly function from C and would be passing a physical address to it. I know the memory region where I can do some stuff in so I do not think that it would be a problem to jump back and forth from protected to real mode.

Quote:
Originally Posted by resetreset View Post
What are SMI Interrupts, btw?
SMM is entered via the SMI (system management interrupt), which is caused by:
* Motherboard hardware or chipset signaling via a designated pin of the processor chip. This signal can be an independent event.
* Software SMI triggered by the system software via an I/O access to a location considered special by the motherboard logic (port 0B2h is common).
* An IO write to a location which the firmware has requested that the processor chip act on.


System Management Mode (SMM) is an operating mode in which all normal execution (including the operating system) is suspended, and special separate software (usually firmware or a hardware-assisted debugger) is executed in high-privilege mode.
 
Old 07-07-2010, 01:19 PM   #7
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Quote:
Originally Posted by yousafsajjad View Post
I would be calling an assembly function from C and would be passing a physical address to it. I know the memory region where I can do some stuff in so I do not think that it would be a problem to jump back and forth from protected to real mode.
Regardless of what "memory region to do some stuff in" you have, you CANNOT go to Real Mode from within Linux. What you're talking about is probably V86 Mode.
 
Old 07-07-2010, 02:24 PM   #8
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
hmm .. i can look into it .. in the mean while .. i have another question .. I have an .obj file which was compiled on windows using masm. Can I link my c file with that .obj file and use it linux???

Last edited by yousafsajjad; 07-07-2010 at 02:31 PM.
 
Old 07-07-2010, 02:38 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by yousafsajjad View Post
hmm .. i can look into it .. in the mean while .. i have another question .. I have an .obj file which was compiled on windows using masm. Can I link my c file with that .obj file and use it linux???
Look up 'binutils' and specifically 'objcopy'.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
masm vaalu Linux - Newbie 1 12-27-2007 04:14 PM
Comiping MASM programs with NASM skie_knite007 Programming 1 06-16-2005 06:59 PM
nasm vs. masm irfanhab Programming 3 10-31-2004 01:28 AM
masm question ody1 Programming 4 06-16-2004 03:34 PM
Looking for a MASM equivelant... ooagentbender Programming 2 02-07-2004 12:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration