-1

I've been trying to increase my baseline knowledge of Intel x86 Nasm by working on outputting a hardcoded text string in an OS/Kernel real mode environment using BIOS interrupts.

Here's my code:

top:
[BITS 16]
[ORG 7C00h]
resb 56 ; Just for testing
outStr db "This is a test string!", 10, 0
resb 56

.main:
main:
mov edi, outStr
again:
mov BYTE al, [edi]
mov ah,0Eh
int 10h
inc edi

cmp BYTE [edi], 0
jg again


leaving:
mov ah, 0Eh

mov al,13
int 10h
mov al,10
int 10h

mov al,'Q' ; Using the distinct character Q to show that the code executed to this point
int 10h

complete:
hlt


times (510-($-top)) db 0 ; Pad the executable to act as the boot sector
dw 0xAA55

I have actually gotten it to work, but only with certain strings. Changing the contents of outStr will sometimes cause the code to not execute at all. At first I thought it was the length, but this proved not to be a determining factor; a small handful of random characters, "Gfd" for example, outputs fine, but "Gfd is awesome!" mysteriously does not work. On the other hand, "ABCDEFGH...Z" (the full capitalized alphabet) did work. To make things even weirder, the full lowercase alphabet did not work.

Anytime it doesn't work, the confirmation character at the end ("Q") is not outputted, and non of the other expected behavior is observed, further leading me to believe that this has nothing to do with my executable code, but with something else.

Important: I've been doing my testing using QEMU. It seems to be a pretty commonly used tool and works for me (with the above exceptions), but I'll be interested to see if this could have some effect on the output.

1
  • 1
    Wow, so much hate. I see now that there was a misconception or two in my code, but I don't think it deserves quite this many downvotes. After all, I'm just learning. Isn't that part of the purpose of this site; to help people further their knowledge in specific areas which they are having trouble with?
    – Gfdking
    Jul 6, 2012 at 18:08

2 Answers 2

4

You're executing your resb's and the string! "jmp" over 'em.

Initialize ds... and perhaps es... and set up a valid stack at a known (safe!) address. It may be that QEMU is starting you up with acceptable values, but you shouldn't count on it. ASSume only that you're loaded at 0x7C00 and that the boot drive number is in dl.

"hlt" only halts the CPU until an interrupt occurs (one per 18.2 seconds for the timer interrupt, among any others), at which point you're executing random code. Throw a "hlt" into an infinite loop if you want to keep the CPU cool...

Happy bootin',

Frank

2
  • 3
    Actually there is smarter way to stop CPU permamently: cli; hlt, that is.
    – Griwes
    Jul 6, 2012 at 17:30
  • in some compilers you can use jmp $ (aor any character with similar sense) to jump 'here'.
    – user35443
    Aug 7, 2012 at 13:26
4

You're obviously executing your string. Put a jump to main at the beginning, or declare your data after the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.