Author Topic: masm to nasm help!  (Read 19820 times)

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
masm to nasm help!
« on: December 06, 2010, 11:08:43 PM »
I have recently switched from masm to nasm and my code I wrote in masm no longer works so i'm trying to recreate it in nasm. My code for masm was:
Code: [Select]
.model small
.stack
.data
.code
AppMain:
    mov ax, 13h
    int 10h

    mov ax, a000h
    mov es, ax

WriteGraphics:
    mov ah, 2
    mov di, cx
    mov cx, 64000

FillScreen:
    mov es:[di], ah
    add di, 1
    cmp di, cx
    jb FillScreen

end AppMain
This would simply fill the screen with green.


Since It no longer works I tryed it in nasm, by writing this:
Code: [Select]
[BITS 16]
[ORG 0x7c00]
AppMain:
mov ax, 00a000h
mov es, ax

mov ah, 1
mov di, 0

FillScreen:
mov dx, es ;___________
add dx, di ; these three instructions used to be "mov es:[di], ah", I changed it because it got an error, Still doesn't work though.
mov [dx], ah ;____________
add di, 1
cmp di, 64000
jb FillScreen

times 510-($-$$) db 0x00
dw 0xaa55
Using this with nasm I got errors what is wrong with it?
« Last Edit: December 07, 2010, 12:10:45 AM by ThatGuy22 »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: masm to nasm help!
« Reply #1 on: December 06, 2010, 11:43:34 PM »
In the future, please post your build command along with the errors that have been generated.

Off the bat, mov [dx], ah is not a valid memory operation in 16-bit Real Mode.

Read x86 Addressing Modes @ Wikipedia for a quick review.

Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: masm to nasm help!
« Reply #2 on: December 07, 2010, 12:14:24 AM »
I do not see what is wrong with my code from looking at the website, can you please point it out or replace parts of the code section that will make this work?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: masm to nasm help!
« Reply #3 on: December 07, 2010, 01:23:58 AM »
You're comparing apples to oranges here, ThatGuy. Your Masm file is apparently intended to be linked(?) into an .exe(?). The Nasm file appears to be a bootsector - "org 7C00h". Easiest thing is probably to "translate" your Masm file into Nasmese. (I have a fair amount of experience at this)

Code: [Select]
; let's make this a .com file
; nasm -f bin -o myfile.com myfile.asm

org 100h  ; this is where dos loads a .com file

section .text

AppMain:
    mov ax, 13h
    int 10h

    mov ax, 0a000h ; numbers must start with a decimal digit
    mov es, ax

WriteGraphics:
    mov ah, 2
    mov di, cx
    mov cx, 64000

FillScreen:
    mov [es:di], ah ; *all* of a memory reference goes between the "[]"
    add di, 1
    cmp di, cx
    jb FillScreen

; now what? just crash?
; let's pause to admire our work.

  mov ah, 0
  int 16h  ; wait for a key

; maybe we should go back to text mode?

  mov ax, 3
  int 10h

; and back to dos

ret

;end AppMain
; Nasm knows you're done when it falls off the end of the file


Code: [Select]
FillScreen:
mov dx, es ;___________
add dx, di ; these three instructions used to be "mov es:[di], ah", I changed it because it got an error, Still doesn't work though.
mov [dx], ah ;____________

Nice try, but it won't work. The simple solution is:

Code: [Select]
mov [es:di], ah

There's a section in the Friendly Manual for folks switching from Masm...

http://www.nasm.us/xdoc/2.09.04/html/nasmdoc2.html#section-2.2

... may or may not help...

What Keith is trying to tell you about addressing modes is that, using 16-bit instructions, an effective address consists of an offset, a base register, and an index register - all optional. Using 16-bit instructions, the "base" registers are bx and bp, the index registers are si and di. That's it! Using 32-bit instructions, any register can be used as a "base" register, and any but esp can be used as an "index" register, and a "scale" can be applied to the index register, multiplying it by 2, 4, or 8. Folks who have "graduated" from 16-bit to 32-bit generally don't want to go back!

There's a "subtlety" to this - you can use 32-bit instructions in 16-bit code!

Code: [Select]
mov [edx], ah

This will assemble, and might actually work, barring a couple of "gotchas". The "limit" field of segment descriptors is honored, even in 16-bit code. This is "normally" set to 0FFFFh in real mode. If the value in edx exceeds this, it causes a Segment Overrun Exception, crashing your program. It is possible to modify this "limit"... but it isn't what you want to do here...

I notice that the Masm version turns the screen green, but in the Nasm version a
"blue screen" would indicate success. I like it! :)

Best,
Frank


Offline ThatGuy22

  • Jr. Member
  • *
  • Posts: 40
Re: masm to nasm help!
« Reply #4 on: December 07, 2010, 02:47:07 PM »
Thanks, what you said to do works. The reason I did that in the first place is because for some reason what I did works in masm, but not nasm.