From: Jim Carlock on
The code below compiles and runs, but the ORG 100h is required,
otherwise some extra garbage gets printed out and things do not line
up properly.

Are there any other things that might be a little more interesting that
can be added and/or changed in any way without getting too much
more complicated?

<code>
;
; Hello world! example.
; Subsystem: DOS
;
; COMPILE_1: nasmw.exe -fbin -ohellow.com hello.asm
; COMPILE_2: nasmw.exe -fbin -ohellow.exe hello.asm
; LINK_1: NOT REQUIRED FOR .com or DOS .exe
; LINK_2:
;
org 100h

lea edx, [sMsg]
mov eax, 0900h
int 21h
; int 20h
mov eax, 4C00h
int 21h

sMsg db "Hello World",0Dh,0Ah,"$"

</code>

--
Jim Carlock


From: SpooK on
On Jul 30, 8:35 pm, "Jim Carlock" <anonym...(a)127.0.0.1> wrote:
> The code below compiles and runs, but the ORG 100h is required,
> otherwise some extra garbage gets printed out and things do not line
> up properly.
>
> Are there any other things that might be a little more interesting that
> can be added and/or changed in any way without getting too much
> more complicated?
>

Interesting... yes... complicated... well... I'll reduce some
complication by posting the source :)

The source below switches to 320x200 (common for DOS games) and turns
the screen into something that resembles a scrambled TV image while
displaying text on the screen. NASM, of course ;)

[BITS 16]
ORG 0x100

;Switch to VGA Mode 13 (320x200)
mov ax,0x0013
int 0x10

;Display "something"
DISPLAY:
xor ax,ax
int 0x1A
mov WORD[TICK],dx
mov bx,0xA000
mov gs,bx
xor cx,cx
..update:
pusha
mov ax,0x1301
mov bx,0x0007
mov cx,MSG.len-MSG
mov dx,0x0D04
mov bp,MSG
int 0x10
mov ax,0x0B00
int 0x21
cmp al,0
je .time
jmp Exit
..time:
xor ax,ax
int 0x1A
cmp dx,WORD[TICK]
je .time
mov WORD[TICK],dx
popa
xor bx,bx
..write:
call Random
mov cx,WORD[RANDNUM]
mov BYTE[gs:bx],cl
inc bx
cmp bx,0xFA00
jne .write
jmp .update

;Wait for key
xor ax,ax
int 0x16
mov ax,0x0100
int 0x21

mov ax,0x0900
mov dx,CHR
int 0x21

;Restore Display to 80x25 and Exit
Exit:
mov ax,0x0002
int 0x10
mov ax,0x4C00
int 0x21

;Generate a Random Number in CL
Random:
pusha
mov ax,WORD[SEED1]
mov bx,WORD[SEED2]
mov cx,ax
mul WORD[CONSTANT]
shl cx,1
shl cx,1
shl cx,1
add ch,cl
add dx,cx
add dx,bx
shl bx,1
shl bx,1
add dx,bx
add dh,bl
mov cl,5
shl bx,cl
add ax,1
adc dx,0
mov WORD[SEED1],ax
mov WORD[SEED2],dx
mov bx,WORD[RANGE]
xor ax,ax
xchg ax,dx
div bx
xchg ax,dx
mov WORD[RANDNUM],ax
popa
ret

MSG DB "Feel my 8-bit epileptic wrath >:@"
..len
CHR DB 0,'$'
TICK DW 0
RANGE DW 0x00FF
RANDNUM DW 0x0000
CONSTANT DW 0x8405
SEED1 DW 0
SEED2 DW 0



From: Jim Carlock on
"SpooK" wrote:
<snip>...</snip>

You wouldn't know how to restore the CMD prompt with that app
you posted, would you? It looks like it's 16-bit, so I'm interested in
switching out of the 16-bit mode, shutting down the ntvdm.exe app
that it creates and restoring the cmd.exe prompt scrollbars. Which I
think is going to involve the following API...

; Function AllocConsole "kernel32.dll" () dw
; Function FreeConsole "kernel32.dll" () dw
; Function CloseHandle "kernel32.dll" (dw:hObject) dw
; Function GetStdHandle "kernel32.dll" (dw:nStdHandle) dw
; Function WriteConsoleA "kernel32.dll" (dw:hConsoleOutput, dw:lpBuffer, \
; dw:nNumberOfCharsToWrite, dw:lpNumberOfCharsWritten, \
; dw:lpReserved) dw
; Function ReadConsoleA "kernel32.dll" (dw:hConsoleInput, dw:lpBuffer, \
; dw:nNumberOfCharsToRead, dw:lpNumberOfCharsRead, \
; dw:lpReserved) dw
; Function SetConsoleTextAttribute "kernel32.dll" (dw:hConsoleOutput, \
; dw:wAttributes) dw
; Function SetConsoleTitleA "kernel32.dll" (dw:lpConsoleTitle) dw
; Function GetLastError "kernel32.dll" () dw

I was thinking of getting the HelloWorld out in a COFF image.

<code name="HelloWorld-coff.asm">
; TRYING coff
;
; COMPILE2: nasmw.exe -fcoff -oHelloWorld-coff.obj HelloWorld-coff.asm
; LINK_05: link700.exe /subsystem:CONSOLE HelloWorld-coff.obj \SDK\lib\kernel32.lib
; LINK_05: Creates: "HelloWorld-coff.exe", "1,536 bytes"
; CRASHES!!!
;
; NOTES
;
; int 21 is 16-bit. This causes the program to crash.
;
; This program needs to employ Console API.
;

; global start
global _mainCRTStartup

section .text
; start:
_mainCRTStartup:
;mov edx,msg
lea edx, [sMsg]
mov eax,0900h
int 21h
mov eax,4C00h
int 21h
section .data
sMsg db 'Hello, World!$'
</code>

It appears that I need to get into the Windows API I listed above, to
get any sort of COFF thing going?

--
Jim Carlock


From: SpooK on
On Jul 30, 9:58 pm, "Jim Carlock" <anonym...(a)127.0.0.1> wrote:
> "SpooK" wrote:
>
> <snip>...</snip>
>
> You wouldn't know how to restore the CMD prompt with that app
> you posted, would you? It looks like it's 16-bit, so I'm interested in
> switching out of the 16-bit mode, shutting down the ntvdm.exe app
> that it creates and restoring the cmd.exe prompt scrollbars. Which I
> think is going to involve the following API...
>
> ; Function AllocConsole "kernel32.dll" () dw
> ; Function FreeConsole "kernel32.dll" () dw
> ; Function CloseHandle "kernel32.dll" (dw:hObject) dw
> ; Function GetStdHandle "kernel32.dll" (dw:nStdHandle) dw
> ; Function WriteConsoleA "kernel32.dll" (dw:hConsoleOutput, dw:lpBuffer, \
> ; dw:nNumberOfCharsToWrite, dw:lpNumberOfCharsWritten, \
> ; dw:lpReserved) dw
> ; Function ReadConsoleA "kernel32.dll" (dw:hConsoleInput, dw:lpBuffer, \
> ; dw:nNumberOfCharsToRead, dw:lpNumberOfCharsRead, \
> ; dw:lpReserved) dw
> ; Function SetConsoleTextAttribute "kernel32.dll" (dw:hConsoleOutput, \
> ; dw:wAttributes) dw
> ; Function SetConsoleTitleA "kernel32.dll" (dw:lpConsoleTitle) dw
> ; Function GetLastError "kernel32.dll" () dw
>
> I was thinking of getting the HelloWorld out in a COFF image.
>
> <code name="HelloWorld-coff.asm">
> ; TRYING coff
> ;
> ; COMPILE2: nasmw.exe -fcoff -oHelloWorld-coff.obj HelloWorld-coff.asm
> ; LINK_05: link700.exe /subsystem:CONSOLE HelloWorld-coff.obj \SDK\lib\kernel32.lib
> ; LINK_05: Creates: "HelloWorld-coff.exe", "1,536 bytes"
> ; CRASHES!!!
> ;
> ; NOTES
> ;
> ; int 21 is 16-bit. This causes the program to crash.
> ;
> ; This program needs to employ Console API.
> ;
>
> ; global start
> global _mainCRTStartup
>
> section .text
> ; start:
> _mainCRTStartup:
> ;mov edx,msg
> lea edx, [sMsg]
> mov eax,0900h
> int 21h
> mov eax,4C00h
> int 21h
> section .data
> sMsg db 'Hello, World!$'
> </code>
>
> It appears that I need to get into the Windows API I listed above, to
> get any sort of COFF thing going?
>
> --
> Jim Carlock

http://www.asmcommunity.net/projects/nasm32/

From: Evenbit on
On Jul 30, 10:58 pm, "Jim Carlock" <anonym...(a)127.0.0.1> wrote:
>
> ; Function AllocConsole "kernel32.dll" () dw
> ; Function FreeConsole "kernel32.dll" () dw
> ; Function CloseHandle "kernel32.dll" (dw:hObject) dw
> ; Function GetStdHandle "kernel32.dll" (dw:nStdHandle) dw
> ; Function WriteConsoleA "kernel32.dll" (dw:hConsoleOutput, dw:lpBuffer, \
> ; dw:nNumberOfCharsToWrite, dw:lpNumberOfCharsWritten, \
> ; dw:lpReserved) dw
> ; Function ReadConsoleA "kernel32.dll" (dw:hConsoleInput, dw:lpBuffer, \
> ; dw:nNumberOfCharsToRead, dw:lpNumberOfCharsRead, \
> ; dw:lpReserved) dw
> ; Function SetConsoleTextAttribute "kernel32.dll" (dw:hConsoleOutput, \
> ; dw:wAttributes) dw
> ; Function SetConsoleTitleA "kernel32.dll" (dw:lpConsoleTitle) dw
> ; Function GetLastError "kernel32.dll" () dw
>
> ; NOTES
> ;
> ; int 21 is 16-bit. This causes the program to crash.
> ;
> ; This program needs to employ Console API.
> ;

The only API calls that you need are GetStdHandle and WriteFile. But
you can avoid those (if you don't mind a bit of bloat) if you employ
the services of a nifty C library. Try this:

[section .data]

hello: db 'Hello, world!',10,0

[section .text]

global _main
extern _printf

_main:
push hello
call _printf
add esp,4

mov eax,0
ret

Assemble this with:

C:\>nasmw -f win32 prog.asm -o prog.obj

Now, link it to GNU's C library with:

C:\>gcc prog.obj -o prog.exe

This comes to you from an interesting tutorial (obligatory PDF
warning) found in the thread located here:
http://www.daniweb.com/forums/thread41309.html

Nathan.

 |  Next  |  Last
Pages: 1 2 3 4
Prev: Linux X demo
Next: where [BITS 32] directive?