Author Topic: - Boot String Input -  (Read 28083 times)

Imran

  • Guest
- Boot String Input -
« on: February 23, 2011, 06:43:44 PM »
Hello everyone, I am quite new to NASM and I am in need of help. I am trying to make a boot program that gets in a string and then prints it back to me. This is what I got so far But I need help. I want to use the "int 21h, ah = 0ah" to do this. I have tried many examples but I have had no luck.

I know the code needs the

Code: [Select]
BITS 16 at the top &

Code: [Select]
times 510-($-$$) db 0 ; Pad remainder of boot sector with 0s
  dw 0xAA55 ; The standard PC boot signature

at the bottom

Any help with sample code would be appreciated. Please dont comment on how dumb I am. Bare in mind I am new to asm since im used to High Level Programming.



Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: - Boot String Input -
« Reply #1 on: February 23, 2011, 08:40:03 PM »
You cannot use functions of the software interrupt 21h in a boot program, because no operating system providing interrupt 21h functions is loaded at the time. Please research elsewhere what functions are available to your boot program. (Hint: Look up software interrupt 10h and software interrupt 16h functions, which are provided by the BIOS in PCs.)
C. Masloch

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: - Boot String Input -
« Reply #2 on: February 23, 2011, 09:14:59 PM »
Well... okay...

int 21h isn't going to be available until/unless you've got dos loaded. If you're still in 16-bit mode, you've got bios interrupts available. int 16h is the one that handles the keyboard. If you've switched to protected mode, bios interrupts won't work, and you'll have to go to ports. Where are you on this thing? You'll need a little more than is shown to get a working bootsector! If you're new to asm, a bootsector is not a particularly easy place to start!

You'll want Ralf Brown's Interrupt List (google "RBIL") if you haven't got it. Besides the eponymous "interrup.lst", "ports.lst" and "memory.lst" will have information you'll need.

Poke around this site:

http://wiki.osdev.org/Main_Page

In particular, look at the "babysteps" tutorial.

This doesn't cover keyboard input in a bootsector. You're familiar with the C function "gets()", right? (and why it's dangerous!) Don't write "gets()"!!! Emulating the int 21h/0Ah interface wouldn't be a bad way to do it, but provide some limit so the user can't overflow the buffer! Emulating the int 21h/40h interface (max chars in cx) might be easier. Shouldn't be a problem in a bootsector, but writing buffer overflows is a really bad habit. We get enough of that from the so-called professionals! (/rant)

If you don't know how to do this, you're not ready for a bootsector (IMO). Write a simple dos .com file first. Use int 21h if it helps you. Then write your own input routine, replacing int 21h - a "dosless .com file". When you've got that working, stuff it into a bootsector (preferably an already-working bootsector). That's how I'd approach it, anyway...

If you need more help, post some of the examples you've tried and we'll see if we can help you figure out what's wrong - but int 21h isn't going to work (unless you've loaded dos (io.sys) in your bootsector, which would be kinda pointless).

Best,
Frank


Imran

  • Guest
Re: - Boot String Input -
« Reply #3 on: February 24, 2011, 05:41:14 AM »
Thanks all for taking me out of the darkness, your help is appreciated. I have anouther question now... How would I go about making a simple DOS, so I can use the int 21h. How would I go about getting out of 16 bit mode?

My goal is a simple terminal OS, but as you can see, I am far from that.

Thanks

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: - Boot String Input -
« Reply #4 on: February 24, 2011, 09:45:21 AM »
That "babysteps" tutorial gets ya into pmode, as I recall...

It is insane to think about int 21h in a bootsector... that's never stopped me...

Code: [Select]
; an attempt to "use int 21h" in a bootsector
; nasm -f bin -o boothw2u.bin boothw2u.asm
; dd if=boothw2u.bin of=/dev/fd0

bits 16

org 7C00h

    jmp short start
    nop
msg db 13, 10, "Please tell me your name? ", 0
msg2 db 13, 10, "Hello, ", 0
nimsg db 13, 10, "Sorry, not implemented!", 13, 10, 0

inbuf db 20h, 0
    times 20h db 0
   
start:
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    cli
    mov ss, ax
    mov sp, ax
    sti
   
    cli
    mov word [21h * 4], my21
    mov [21h * 4 + 2], ds
    sti
   
    mov dx, msg
    mov ah, 9
    int 21h
   
    mov dx, inbuf
    mov ah, 0Ah
    int 21h
   
    mov dx, msg2
    mov ah, 9
    int 21h
   
    mov dx, inbuf + 2
    mov ah, 9
    int 21h
   
blackhole:
    jmp blackhole
;------------------------------   

my21:
    pusha
       
    cmp ah, 9
    jne .not9
    call my21_9
    jmp .done
.not9:
    cmp ah, 0Ah
    jne .notA
    call my21_A
    jmp .done
.notA:
   
    mov dx, nimsg
    call my21_9

.done:
    popa
    iret

my21_9:
    xchg si, dx
    mov ah, 0Eh
    mov bx, 7
.top:
    lodsb
    test al, al
    jz .done
    int 10h
    jmp .top
.done:
    ret

my21_A:
    mov di, dx
    xor cx, cx
    mov bx, 7
    mov cl, [di]
    add di, 2
.top:
    dec cl
    jz .done
    mov ah, 0
    int 16h
    mov ah, 0Eh
    int 10h
    cmp al, 13
    jz .done
    stosb
    inc ch
    jmp .top
.done:
    mov al, 0
    stosb
    mov di, dx
    mov [di + 1], ch
    ret

times 510 - ($ - $$) db 0
db 55h, 0AAh

Sorry for the lack of comments - it was a real quickie job. I don't know if it's right, but it "works for me".

Later,
Frank


Imran

  • Guest
Re: - Boot String Input -
« Reply #5 on: February 24, 2011, 12:56:59 PM »
That "babysteps" tutorial gets ya into pmode, as I recall...

It is insane to think about int 21h in a bootsector... that's never stopped me...

Code: [Select]
; an attempt to "use int 21h" in a bootsector
; nasm -f bin -o boothw2u.bin boothw2u.asm
; dd if=boothw2u.bin of=/dev/fd0

bits 16

org 7C00h

    jmp short start
    nop
msg db 13, 10, "Please tell me your name? ", 0
msg2 db 13, 10, "Hello, ", 0
nimsg db 13, 10, "Sorry, not implemented!", 13, 10, 0

inbuf db 20h, 0
    times 20h db 0
   
start:
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    cli
    mov ss, ax
    mov sp, ax
    sti
   
    cli
    mov word [21h * 4], my21
    mov [21h * 4 + 2], ds
    sti
   
    mov dx, msg
    mov ah, 9
    int 21h
   
    mov dx, inbuf
    mov ah, 0Ah
    int 21h
   
    mov dx, msg2
    mov ah, 9
    int 21h
   
    mov dx, inbuf + 2
    mov ah, 9
    int 21h
   
blackhole:
    jmp blackhole
;------------------------------   

my21:
    pusha
       
    cmp ah, 9
    jne .not9
    call my21_9
    jmp .done
.not9:
    cmp ah, 0Ah
    jne .notA
    call my21_A
    jmp .done
.notA:
   
    mov dx, nimsg
    call my21_9

.done:
    popa
    iret

my21_9:
    xchg si, dx
    mov ah, 0Eh
    mov bx, 7
.top:
    lodsb
    test al, al
    jz .done
    int 10h
    jmp .top
.done:
    ret

my21_A:
    mov di, dx
    xor cx, cx
    mov bx, 7
    mov cl, [di]
    add di, 2
.top:
    dec cl
    jz .done
    mov ah, 0
    int 16h
    mov ah, 0Eh
    int 10h
    cmp al, 13
    jz .done
    stosb
    inc ch
    jmp .top
.done:
    mov al, 0
    stosb
    mov di, dx
    mov [di + 1], ch
    ret

times 510 - ($ - $$) db 0
db 55h, 0AAh

Sorry for the lack of comments - it was a real quickie job. I don't know if it's right, but it "works for me".

Later,
Frank




Thanks alot, The code works perfectly. You are a NASM genius :)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: - Boot String Input -
« Reply #6 on: February 25, 2011, 10:20:37 PM »
Heh! "Idiot savant", maybe. This really isn't "about Nasm" so much as knowing how segmented memory works, and the bios interrupts. Once you switch to protected mode, bios interrupts won't work anymore - you'll need to find an alternative (ports, generally). Poke around that osdev site - tons of useful information!

You'll notice that my example isn't "just like" dos. I used zero-terminated strings instead of that silly(?) '$'-terminated string for int 21h/9, and returned a zero-terminated string from int 21h/0Ah. Seemed "more sensible". You can easily fix it to be more like dos, if that's what you want.

The only sane thing for a bootsector to do is load something else. You'd normally install any "dos-like" interrupts, or whatever, in the "something else". I just did it this way to see if I could... :)

Best,
Frank


zaminur143

  • Guest
Re: - Boot String Input -
« Reply #7 on: March 14, 2011, 06:51:41 AM »
 I have bad luck because i have the same problem,but i have a good luck because i found this post and answer already. Thank for sharing.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: - Boot String Input -
« Reply #8 on: March 14, 2011, 07:24:42 AM »
Okay... but, "You can't use int 21h in a bootsector!" is a better answer.

Best,
Frank


Offline JackTDawson

  • New Member
  • Posts: 1
Re: - Boot String Input -
« Reply #9 on: June 11, 2011, 06:28:20 AM »
Just thought I would throw my two cents into this.

If your making your own Operating System from scratch, the Interrupt 21 doesn't exist. Microsoft made their own INT 21 for their own specific DOS. Since your making your own "DOS" so to speak you have to make your own version of an interrupt. Not needed, but can be useful.

IF you really want to dig into this, try what I did and buy a few books. All these books are very cheap on amazon.com.

Get a copy of the following :

Programmer's Guide to the EGA, VGA, and SuperVGA Cards ( 3rd Edition Ferraro )
PC Interrupts ( First Edition Ralph Brown and this book you can get off of Amazon as well so that you do not have to look them up on a website if you do not want too )
The 80x86 IBM PC and Compatible Computers - Assembly Language, Design, and Interfacing ( Volume 1 and 2 4th Edition )


Those are the three I recommend if your truly wanting to dig into OS creation. I own a copy of all three and they are some of the best books on this subject in my humble opinion. If your doing this just as a hobby, believe it or not, NASM might be too hard for you to start out in and you might want to check out MASM. The only drawback about MASM is your allowed to make an OS only if its educational use only. Your not allowed to sell your OS if you use MASM. ( NOTE : This is only if your making an OS. If you make software for windows using MASM you CAN sell what you create. MS doesn't allow the creation of an OS to be sold because it is competition. ) However NASM you can do what you want. Again, use MASM until your comfortable enough to know what your doing, and NASM for more advanced stuff and freedom to do what you want.

These are just my personal opinions and experience in my own path of this knowledge. Not everyone here will share my way of doing it. These are just suggestions.

Good luck.
« Last Edit: June 11, 2011, 06:34:04 AM by JackTDawson »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: - Boot String Input -
« Reply #10 on: June 11, 2011, 01:00:49 PM »
Can you show us your Masm version of the above, so we can see how much easier it is?

Best,
Frank


Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: - Boot String Input -
« Reply #11 on: June 11, 2011, 01:30:16 PM »
If your doing this just as a hobby, believe it or not, NASM might be too hard for you to start out in and you might want to check out MASM.

I completely disagree with this statement.  Whether the software to be developed is for hobby or for commercial purposes Nasm is not harder - only different.  The places where MASM used to be considered easier are function calls, prologue/epilogue creation and teardown, argument passing, and a few other macros.  Today you can find very similar constructs when using the NASMX macro package which was  designed precisely for those purposes.  Finally, unlike MASM, Nasm can be used for both Linux and Windows development and is unencumbered by licensing restrictions.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: - Boot String Input -
« Reply #12 on: June 11, 2011, 07:30:26 PM »
If you want to see an example of partial INT 21H implementation outside of DOS, take a look at COMBOOT from The Syslinux Project.

If your doing this just as a hobby, believe it or not, NASM might be too hard for you to start out in and you might want to check out MASM.

I completely disagree with this statement.

I also completely disagree with that statement.

The claimed strengths of MASM, I've found to be crippling weaknesses with regard to OS development in particular. MASM simply gets in the way with certain ambiguous syntax and implicit functionality. When dealing with critical code, you want to eliminate as many assumptions as possible, including any that the assembler impose.

Also, prior to software such as Wine, MASM wouldn't even run outside of DOS/Windows, where-as NASM is about as cross-platform as a program can get.

Moreover, and quite importantly, you have to make sure and read the EULA for whatever version of MASM you are using to avoid any legalities in how you are using MASM. You can get a decent summary of certain MASM EULA quirks on the MASM Page @ OSDev.org Wiki.

And, lastly, MASM is simply dying of neglect. Microsoft is heavily invested in .NET and MASM isn't even a blip on the radar screen. If you really want to invest in MASM-like syntax, I'd jump on the JWasm bandwagon instead.