NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: ThatGuy22 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:
.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:
[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?
-
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 (http://en.wikipedia.org/wiki/X86#Addressing_modes) for a quick review.
-
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?
-
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)
; 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
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:
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!
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
-
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.