NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: computertrick on February 19, 2010, 12:20:07 AM

Title: Could someone give me a few code examples and explain them?
Post by: computertrick on February 19, 2010, 12:20:07 AM
ok basicly i need a few examples and i need them explaned, they also have to be made for DOS

also could one of these examples be a thing that displays a message then after a key is pressed it will send a diffrent message? thanks  :)
Title: Re: Could someone give me a few code examples and explain them?
Post by: Frank Kotler on February 19, 2010, 01:16:34 AM
Too easy! You know "hello world", right? (or don't you?) Just do it, wait for a key (int 21h/7 or 8, IIRC) and do it again with a different message in dx. Try this one:

Code: [Select]
;--------------------------------------------
; get (text) input from user and echo it back
;
; nasm -f bin -o hijoe.com hijoe.asm
;---------------------------------------------

org 100h       ; we're a .com file - inform Nasm that
               ; we'll be running at xxxx:0100

; Cultures differ. In some places a person's full
; name might honor all eight great-great grandparents
; and a couple of saints. From other areas - like
; Afghanistan - we hear, "Lulu, who uses only one
; name, says shes back in town." If your name's longer
; than "Lulu", you'll want to increase this number. I've
; deliberately left it short, to observe the behaviour if
; the user tries to exceed it. Note that with MAX_INPUT = 5,
; you can only enter 4 characters! The final carriage-return
; when the user hits "Enter" is part of the maximum count,
; but not in the count of characters entered (returned in
; [buffer + 1]). When you're done observing, increase this
; value to something sensible.

MAX_INPUT equ 5

section .data
    msg1 db 13, 10, 'Please tell me your name: $'
    msg2 db 13, 10, 'Hello, $'

    ; note the double quotes, to allow the apostrophes
    msg3 db "! How're ya doin'?", 13, 10, '$'

section .bss

; Using int 21h/0Ah for input, we need a buffer at least
; two bytes longer than the maximum allowed input - one
; for the "maximum count", and one for the returned
; "actual count". The actual text entered will begin at
; the third byte, [buffer + 2]. I've allowed one extra,
; so we could terminate the string *after* the carriage
; return - we don't do that here, so +2 would be enough.

    input_buffer resb MAX_INPUT + 3

section .text

; first, print the prompt.
; this is just "hello world" again

    mov ah, 9
    mov dx, msg1
    int 21h

; int 21h/0Ah expects the first byte of its input buffer
; to be the maximum number of characters to allow, and
; won't work without it. So put it there.

    mov byte [input_buffer], MAX_INPUT

; and call dos' "buffered input function" - subfunction 0Ah

    mov ah, 0Ah
    mov dx, input_buffer
    int 21h

; Now we've got the input, starting at [buffer + 2], but it
; isn't terminated in any useful way. Zero-termination is more
; generally useful - a filename to open, for example - but
; int 21h/9 wants the damfool '$', so do it.

; make sure bh is zeroed

    xor bx, bx

; count of characters entered is at [buffer + 1]

    mov si, input_buffer + 1

; fetch it into bl (bx)

    mov bl, [si]

; overwrite the carriage return at the end of input

    mov byte[si + bx + 1], '$'

; print the first part of our message

    mov ah, 9
    mov dx, msg2
    int 21h

; users text starts at buffer + 2. Note: no need to reload
; ah with 9, it stays there...

    mov dx, input_buffer + 2
    int 21h

; and the last part of our message

    mov dx, msg3
    int 21h

; and exit back to dos

    mov ah, 4Ch
    int 21h
;----------------------------

If you're going to mess with dos, you'll want Ralf Brown's Interrupt List (RBIL). Google for it.

Best,
Frank