whats wrong with this [nasm](solved)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
aliceinchains
Posts: 14
Joined: Wed Aug 13, 2014 11:04 am

whats wrong with this [nasm](solved)

Post by aliceinchains »

Code: Select all

use16

mov ax, sp
mov [stackpointer], ax

keyloop:
	mov ah, 0
	int 0x16

	cmp al, 0x08
	je .bkspc

	cmp al, 0x0d
	je .ret

	call print_al
	call cursor_forward
	push ax
	jmp keyloop
.bkspc:
	call cursor_backward
	mov al, ' '
	call print_al
jmp keyloop
.ret:
	call cmdcmp
	call scroll_up
jmp keyloop

stackpointer dd 0
cmdstr db 'cls help',0
cmdsame db 1
cmdspcs db 0
cmdcmp:
	xor eax, eax
	mov ds, ax
	mov es, ax
	mov esi, [stackpointer]
	mov edi, cmdstr
	add esi, 1
	mov byte [cmdsame], 1
	mov byte [cmdspcs], 0
	cld
.loop:
	cmpsb
	add esi, 1
	je .loop
	
	cmp byte [edi], 32
	je .spc

	cmp byte [edi], 0
	je .end

	mov byte [cmdsame], 0
jmp .loop
.spc:
	cmp byte [cmdsame], 1
	je .match
	mov byte [cmdsame], 1
	add byte [cmdspcs], 1
	mov esi, [stackpointer]
	add esi, 1
jmp .loop
.end:
	mov ax, [stackpointer]
	mov sp, ax
	cmp byte [cmdsame], 1
	je .match
ret
.match:
	mov ax, [stackpointer]
	mov sp, ax
	cmp byte [cmdspcs], 0
	je .cls
	cmp byte [cmdspcs], 1
	je .help
ret
.cls:
	mov ah, 0x0f
	int 0x10
	mov ah, 0x00
	int 0x10
	mov ah, 2
	mov bh, 0
	mov dh, 0
	mov dl, 0
	int 0x10
ret
.help:
	
ret
print_al:
	mov ah, 9 ;write char and attr at cursor
	mov bh, 0 ;page
	mov bl, 0x0f ;color
	mov cx, 1 ;write times
	int 0x10
ret
cursor_getpos:
	mov ah, 3
	int 0x10
ret
cursor_forward:
	call cursor_getpos
	mov ah, 2 ;set cursor pos
	cmp dl, 79
	je .colmax
	add dl, 1
	int 0x10
ret
.colmax:
	cmp dh, 24
	je .rowmax
	mov dl, 0
	add dh, 1
	int 0x10
ret
.rowmax:
	mov dl, 0
	int 0x10
	call scroll_down
ret
cursor_backward:
	call cursor_getpos
	mov ah, 2 ;set cursor pos
	cmp dl, 0
	je .colmin
	sub dl, 1
	int 0x10
ret
.colmin:
	cmp dh, 0
	je .rowmin
	mov dl, 79
	sub dh, 1
	int 0x10
ret
.rowmin:
	mov dl, 79
	int 0x10
	call scroll_up
ret
scroll_up:
	mov ah, 6
	mov al, 1
	mov bh, 0x0f
	mov cx, 0
	mov dx, 0x4f4f
	int 0x10
ret
scroll_down:
	mov ah, 7
	mov al, 1
	mov bh, 0x0f
	mov cx, 0
	mov dx, 0x4f4f
	int 0x10
ret
89 E0 A3 2C 8E B4 00 CD 16 3C 08 74 0D 3C 0D 74
13 E8 B3 00 E8 C1 00 50 EB EB E8 E0 00 B0 20 E8
A5 00 EB E1 E8 14 00 E8 F8 00 EB D9 00 00 00 00
63 6C 73 20 68 65 6C 70 00 01 00 66 31 C0 8E D8
8E C0 66 8B 36 2C 8E 66 BF 30 8E 00 00 66 83 C6
01 C6 06 39 8E 01 C6 06 3A 8E 00 FC A6 66 83 C6
01 74 F9 67 80 3F 20 74 0D 67 80 3F 00 74 23 C6
06 39 8E 00 EB E6 80 3E 39 8E 01 74 22 C6 06 39
8E 01 80 06 3A 8E 01 66 8B 36 2C 8E 66 83 C6 01
EB CA A1 2C 8E 89 C4 80 3E 39 8E 01 74 01 C3 A1
2C 8E 89 C4 80 3E 3A 8E 00 74 08 80 3E 3A 8E 01
74 14 C3 B4 0F CD 10 B4 00 CD 10 B4 02 B7 00 B6
00 B2 00 CD 10 C3 C3 B4 09 B7 00 B3 0F B9 01 00
CD 10 C3 B4 03 CD 10 C3 E8 F8 FF B4 02 80 FA 4F
74 06 80 C2 01 CD 10 C3 80 FE 18 74 08 B2 00 80
C6 01 CD 10 C3 B2 00 CD 10 E8 35 00 C3 E8 D3 FF
B4 02 80 FA 00 74 06 80 EA 01 CD 10 C3 80 FE 00
74 08 B2 4F 80 EE 01 CD 10 C3 B2 4F CD 10 E8 01
00 C3 B4 06 B0 01 B7 0F B9 00 00 BA 4F 4F CD 10
C3 B4 07 B0 01 B7 0F B9 00 00 BA 4F 4F CD 10 C3
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I can post what runs this just wasn't sure if it was obvious...
Last edited by aliceinchains on Sat May 21, 2016 8:26 am, edited 1 time in total.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: whats wrong with this [nasm]

Post by kzinti »

What's wrong is that you aren't telling us what you want with this code...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: whats wrong with this [nasm]

Post by Brendan »

Hi,

I'm not sure what it's supposed to do, or what it actually does. However..

a) If the user presses too many keys you'll overflow the stack and either corrupt data or crash.

b) It seems to be pushing (16-bit) words onto the stack and then expecting the stack to contain (8-bit) characters.

c) It assumes the stack grows up. It doesn't. Each time you push something on the stack SP decreases, so the data ends up from "[stackpointer]" down towards lower addresses and not from "[stackpointer]" up to higher addresses.

To work around the last problem; you could use "STD" instead of "CLD" before the "CMPSB" and change the string literals to reverse order, like this:

Code: Select all

    db 0,"pleh slc"
cmdstr:
A much better idea is to stop using the stack to store characters and use a normal buffer in RAM instead (which would also help with the other problems)

I'd also recommend not using BIOS functions for video (they're very slow) and write to display memory yourself, and having a double buffer in RAM so that scrolling doesn't involve reading from display memory.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: whats wrong with this [nasm]

Post by mikegonta »

Brendan wrote:I'd also recommend not using BIOS functions for video (they're very slow) and write to display memory yourself ....
Writing character/attribute values to display memory displays nothing. The firmware aka the very slow video BIOS interprets these values
to display pixels on screen. The small additional overhead of the int 0x10 functions is negligible compared to the firmware overhead.
Surely you are not suggesting that a beginner should be displaying the characters with a frame buffer and pixel bashing.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: whats wrong with this [nasm]

Post by Brendan »

Hi,
mikegonta wrote:
Brendan wrote:I'd also recommend not using BIOS functions for video (they're very slow) and write to display memory yourself ....
Writing character/attribute values to display memory displays nothing. The firmware aka the very slow video BIOS interprets these values
to display pixels on screen. The small additional overhead of the int 0x10 functions is negligible compared to the firmware overhead.
I assumed that it's very likely that aliceinchains is using text mode, where the video card converts character/attribute values into video signals itself *without* any firmware involved. In this case the overhead of a (software) interrupt followed by the overhead of multiple layers of "if function number ...." branches dominates and it's significantly faster (and extremely easy) to avoid the BIOS.

For graphics modes, the BIOS functions are typically significantly worse (especially for scrolling), and it's even more important to avoid using the BIOS (even if that means implementing a trivial loop to convert characters into pixels yourself).
mikegonta wrote:Surely you are not suggesting that a beginner should be displaying the characters with a frame buffer and pixel bashing.
I am. Please note that a beginner OS developer is not supposed to be a beginner programmer (but even for beginner programmers figuring out how to do graphics would be worthwhile just to get more experience programming if not for any other reason).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: whats wrong with this [nasm]

Post by mikegonta »

Brendan wrote:I assumed that it's very likely that aliceinchains is using text mode, where the video card converts character/attribute values into video
signals itself *without* any firmware involved.
Then obviously all those overclockers who flash upgrade their GPU BIOS as this How To site (as well as many other such sites) explains,
are completely wasting their efforts. (I am of course assuming that the GPU is emulating the ancient VGA and that there is in fact not a
separate VGA chip side by side on the same silicone).
Brendan wrote:
mikegonta wrote:Surely you are not suggesting that a beginner should be displaying the characters with a frame buffer and pixel bashing.
I am. Please note that a beginner OS developer is not supposed to be a beginner programmer (but even for beginner programmers
figuring out how to do graphics would be worthwhile just to get more experience programming if not for any other reason).
I agree.
However, in this case the OP is more concerned with getting some simple aspects of OS dev working (which requires screen display)
and not specifically with the more advanced ways and means of doing so. Actually getting something working is much better for the
beginner (a few extra milliseconds notwithstanding) then dealing with complexities that the professional takes for granted.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: whats wrong with this [nasm]

Post by Brendan »

Hi,
mikegonta wrote:
Brendan wrote:I assumed that it's very likely that aliceinchains is using text mode, where the video card converts character/attribute values into video
signals itself *without* any firmware involved.
Then obviously all those overclockers who flash upgrade their GPU BIOS as this How To site (as well as many other such sites) explains,
are completely wasting their efforts. (I am of course assuming that the GPU is emulating the ancient VGA and that there is in fact not a
separate VGA chip side by side on the same silicone).
There are multiple reasons to want to change/update a video card's ROM (support for UEFI, support for more video modes, etc); but none of the reasons have anything to do with any "firmware converts character/attribute to pixels to emulate text mode" nonsense.

A modern video card does have multiple logical pieces in the same silicon. Among them is a part typically called something like "display interface" which controls video modes and VGA/DVI/HDMI ports (and has nothing at all to do with GPU), and this "display interface" logic almost always has VGA compatibility and includes the logic for hardware supported text modes that has existed since MDA and CGA video cards in the early 1980s.

As soon as nobody cares about "legacy BIOS" any more (and given that both major OSs for 80x86 PCs require UEFI this is likely to be soon) hardware manufacturers can start ripping out legacy baggage (A20, PIC, PIT, PS/2 controller, etc) and this includes removing hardware support for text modes in video cards. The "display interface" logic for handling text modes will continue to exist until then (and after its removed there will be no text modes at all).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply