From: playstationdude on
hey everyone
I'm a CS student who's learning x86 assembly language right now. The
book we're using, Assembly Language for Intel-Based Computers by Kip
Irvine is written for MASM. Not owning a Windows computer, I cannot
use MASM. The preface of the book said it is easy to convert the code
from MASM to TASM or NASM, but hard to for the GNU assembler. My
instructor said he didn't know how to do this, but he would be fine if
I could figure out how to do it. Does anyone know anything about
this? Thanks!

From: cr88192 on

"playstationdude" <playstationdude(a)gmail.com> wrote in message
news:1189141795.800646.158630(a)r34g2000hsd.googlegroups.com...
> hey everyone
> I'm a CS student who's learning x86 assembly language right now. The
> book we're using, Assembly Language for Intel-Based Computers by Kip
> Irvine is written for MASM. Not owning a Windows computer, I cannot
> use MASM. The preface of the book said it is easy to convert the code
> from MASM to TASM or NASM, but hard to for the GNU assembler. My
> instructor said he didn't know how to do this, but he would be fine if
> I could figure out how to do it. Does anyone know anything about
> this? Thanks!
>

not too hard, but not trivial either.

easier if you are using code that does not use a lot of masm-specific
builtin cruft.

maybe, find some examples of nasm code, and look at that.

note that, beyond this, dos, windows, and linux, are very different OS's,
so, depending on what you are doing, it may take a lot more modification
than just converting between assemblers to make it work...


here are a few major differences (sorry if my MASM examples have errors, as
I haven't really use MASM in over a decade):
NASM does not use 'PROC' or similar directives, you have to do all this
yourself (noting where things are on the stack, ...).

NASM does not infer memory references.

in MASM, one would type:
mov eax, foo
but, in NASM, this is:
mov eax, [foo]

and, in MASM:
mov eax, offset bar
is, in NASM:
mov eax, bar

....

gas, yes, is a bit harder:
mov eax, ecx
becomes:
movl %ecx, %eax
....

or such...



From: Rod Pemberton on

"playstationdude" <playstationdude(a)gmail.com> wrote in message
news:1189141795.800646.158630(a)r34g2000hsd.googlegroups.com...
> hey everyone
> I'm a CS student who's learning x86 assembly language right now. The
> book we're using, Assembly Language for Intel-Based Computers by Kip
> Irvine is written for MASM. Not owning a Windows computer, I cannot
> use MASM. The preface of the book said it is easy to convert the code
> from MASM to TASM or NASM, but hard to for the GNU assembler. My
> instructor said he didn't know how to do this, but he would be fine if
> I could figure out how to do it. Does anyone know anything about
> this? Thanks!
>

About a,b,c, or d?
a) MASM (Intel syntax) to TASM
?
b) MASM (Intel syntax) to NASM (NASM syntax)
1) drop the "t/d/q/word ptr"
2) add size specifiers "byte/word/dword"
3) put memory references in brackets []
4) ? other...
c) MASM (Intel syntax) vs. GAS (AT&T syntax)
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html
d) all of the above


There are some programs out there that do conversions between a few of the
[plural of syntax: syntaxes, syntaces, ?]. Searching for you, I find:

NOMYSO (perl MASM/TASM->NASM) http://www.devoresoftware.com/nomyso/
Intel2GAS (NASM->GAS) http://www.niksula.cs.hut.fi/~mtiihone/intel2gas/
A86CNVRT (MASM->A86)
ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/a86cnvrt.zip
as386.sed (MASM->GAS)
http://www.delorie.com/djgpp/faq/converting/asm2s-sed.html

I seem to recall seeing some others, but didn't run across them just now.


Rod Pemberton

From: Frank Kotler on
playstationdude wrote:
> hey everyone
> I'm a CS student who's learning x86 assembly language right now. The
> book we're using, Assembly Language for Intel-Based Computers by Kip
> Irvine is written for MASM. Not owning a Windows computer, I cannot
> use MASM. The preface of the book said it is easy to convert the code
> from MASM to TASM or NASM, but hard to for the GNU assembler. My
> instructor said he didn't know how to do this, but he would be fine if
> I could figure out how to do it. Does anyone know anything about
> this? Thanks!

Well... I've converted a fair amount of code from Masm syntax to Nasm
syntax - but for a "Windows computer"... although dos code, mostly.
Porting code intended for dos/doze (apparently the book does both?)
to... your header indicates Linux... that's a different matter!
According to the blurb at Amazon, the 16-bit stuff will run under
dosemu. (I haven't had much luck with graphics programs under dosemu)
The 32-bit stuff just might run under WINE. Have you got WINE installed
and running? I tried it the other day, and just got error messages.

I can get the examples and libraries from Mr. Irvine's site... in .exe
format. :( From what I can see, the code you'll be writing does stuff like:

TITLE MASM Template (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc

..data
myMessage BYTE "MASM program example",0dh,0ah,0

..code
main PROC
call Clrscr

mov edx,OFFSET myMessage
call WriteString

exit
main ENDP

END main

We could go through that line-by-line and discuss what would need to be
changed to get it to assemble with Nasm (or Gas), but it wouldn't run.
The code in the libraries you call is completely wrong. WriteString
takes the offset (address) of the string to print in edx. Unless I'm
mistaken, it'll eventually call WriteFile, with "stdout" as a handle (in
Windows, we'll need to "get" that handle if we haven't got it)...
parameters will be passed on the stack... WriteFile wants to know the
length of the string, so we'll need to find that, and push it in the
right place. WriteFile wants the address of a place to return how many
bytes were actually written - probably make a temporary variable on the
stack for that.

For Linux, our WriteString function would be quite different. We still
need the length of the string, we know the handle for stdout... We'll be
using "sys_write", not WriteFile... returns bytes written in eax. A bit
simpler, actually. Sys_write wants the address of the string in ecx, not
edx - easily fixed:

mov ecx, edx
; now we want the length in edx
xor edx, edx

getlen:
cmp byte [ecx + edx], 0
jz gotlen
inc edx
jmp getlen
gotlen:

; file handle to write to in ebx - stdout is 1
mov ebx, 1

; system call number in eax - __NR_write is 4
mov eax, 4

; call the OS
int 80h

ret

Now if we put that code in a file ("decorated" a little bit), assembled
it, and put the resulting file into a library (along with Clrscr and
exit), the example - suitably "translated", assembled, and linked agaist
that library - would work in Linux (barring errors).

I don't expect you, as a beginner, to do all that. If there were only a
few routines in the library, I'd offer to attempt to make a Linux
version, but I expect the number of routines exceeds my ambition (quite
small). I hate to tell you to run out and buy Windows. I hate to tell
you to drop the course. I hate to tell you... you're between a rock and
a hard place. If WINE will work with this stuff, that might be your
salvation. Otherwise... not easy. (but perhaps doable if you're
determined enough)

Best,
Frank