Page 1 of 2

ASM-print wont work right

Posted: Wed Jan 29, 2003 1:19 pm
by FlashBurn
I?ve written a print function, but it wont work right. The problem is that it prints 1 line right and then sometimes do nothing on the screen and sometimes it prints 3 lines and then it starts in the first of the 3 lines. I think that the problem is my getxy function.

[attachment deleted by admin]

Re:ASM-print wont work right

Posted: Wed Jan 29, 2003 1:20 pm
by FlashBurn
This is a screenshot of one problem. It prints till where the cursor is and then starts in the first line of the "" filled lines.

[attachment deleted by admin]

Re:ASM-print wont work right

Posted: Wed Jan 29, 2003 4:11 pm
by drizzt
The code you have posted reprograms the PIC... I don't see any print code here!!!

Re:ASM-print wont work right

Posted: Wed Jan 29, 2003 5:46 pm
by FlashBurn
Ups ::) So now it should be the right file.

[attachment deleted by admin]

Re:ASM-print wont work right

Posted: Wed Jan 29, 2003 7:33 pm
by Curufir
Need some more info.

What is it you're expecting to see? Just a single backslash, a line of backslashes? You haven't actually said what you're trying to print

Re:ASM-print wont work right

Posted: Thu Jan 30, 2003 1:59 am
by FlashBurn
No, My problem is that he prints only this 4 lines. If I print a string he also will print it in this 4 lines. I think the problem is the getxy function. This with the "\" was only a test to look where it goes into the first of the 4 lines. But if you will so, I have a endless loop and want to print "\" but he only print it in this 4 lines. I hope that you could understand my stupid english!

Re:ASM-print wont work right

Posted: Thu Jan 30, 2003 5:53 am
by Pype.Clicker
seeing so much asm code without a push/pop looks strange. are you sure your precious ESI isn't clobbered somewhere ?
BTW, where is the code that calls print ?

Re:ASM-print wont work right

Posted: Thu Jan 30, 2003 6:07 am
by FlashBurn
The "esi" register is not hte problem, believe me! What do you mean "where is the code that calls print"?

Re:ASM-print wont work right

Posted: Thu Jan 30, 2003 6:17 am
by Pype.Clicker
i mean that you give us a print function without the code that invokes it. how can we check your calling code is okay and your data match what we see ?
You say "believe me" ... i believe what i see ...

Re:ASM-print wont work right

Posted: Thu Jan 30, 2003 6:28 am
by FlashBurn
Ok here is the data :

Code: Select all

loop:
mov esi,test
call print

jmp loop

test  db '\ ',0
Is it enough or should I upload my whole os source?!

Re:ASM-print wont work right

Posted: Thu Jan 30, 2003 7:11 am
by Pype.Clicker

Code: Select all

get_offset:
   call getxy

   mov bx,ax
   mov al,160
   mul bl
   shl bh,1      ;;; ??
   xchg bl,bh   ;;; ??
   xor bh,bh    ;;; ??
   add ax,bx

   mov edi,0b8000h
   add di,ax

   ret
those operations on bx seems weird. You'll end up with
bx == ((cursor.x*2)&0xFF)

i suggest you do

Code: Select all

   shr bx,7
instead

btw, it seems very inefficient to change the cursor place and re-read it as much as you do. You have a lot of div/mul instructions that could be removed if you only had start_current_line_offset variable. The X,Y coordinates of the cursor could also be stored in RAM so that you don't have to getXY() every time you start another printc().

remember that I/O access is alot slower than (cached) memory access...

Re:ASM-print wont work right

Posted: Sun Feb 02, 2003 10:46 am
by FlashBurn
This is my new working print function. Maybe you know some optimations.

Code: Select all

;----------------------------
;   Input:
;   ESI = pointer to 0-terminated string
print:
   call getxy
   mov [cur_y],al
   mov [cur_x],ah
   
   xor cx,cx
   mov cl,ah
   shl cl,1
   
   mov bl,160
   mul bl
   
   add ax,cx
   
   mov edi,0b8000h
   add di,ax
.loop
   lodsb
   
   and al,al
   jz near .end
   
   cmp al,8
   je .tab
   
   cmp al,9
   je .back
   
   cmp al,10
   je near .new_line
   
   mov ah,7
   stosw
   
   cmp byte[cur_x],79
   je .new_line
   
   inc byte[cur_x]
   
   jmp .loop
.tab
   mov al,' '
   mov ah,7
   mov ecx,4
   rep stosw
   
   add byte[cur_x],4
   
   cmp byte[cur_x],79
   jng .loop
   
   mov al,[cur_x]
   sub al,4
   mov ah,79
   sub ah,al
   mov [cur_x],ah
   inc byte[cur_y]    

   jmp .loop
.back
   cmp byte[cur_x],0
   jne .back1
   
   cmp byte[cur_y],0
   je .loop
   
   mov byte[cur_x],80
   dec byte[cur_y]
.back1
   dec byte[cur_x]
   sub edi,2
   mov al,' '
   mov ah,7
   stosw
   sub edi,2
   
   jmp .loop
.new_line
   xor al,al
   mov [cur_x],al
   
   cmp byte[cur_y],24
   je .scroll_down
   
   inc byte[cur_y]
;----------------------------   
   xor cx,cx
   mov cl,ah
   shl cl,2
   
   mov bl,160
   mul bl
   
   add ax,cx
   
   mov edi,0b8000h
   add di,ax
;----------------------------   
   jmp .loop
.scroll_down
   push esi
   call scroll_down
   pop esi
   
   mov edi,0b8f00h
   
   jmp .loop
   
.end
   mov al,[cur_y]
   mov ah,[cur_x]
   call gotoxy
   
   ret
;----------------------------
;   vars
cur_y   db 0
cur_x   db 0
;----------------------------

Re:ASM-print wont work right

Posted: Sun Feb 02, 2003 3:21 pm
by FlashBurn
There is a problem on real PC?s now!? It seems like the x-coord and the y-coord are exchanged. Is this possible?

Re:ASM-print wont work right

Posted: Sun Feb 02, 2003 11:12 pm
by FlashBurn
I fixed the problem. Now the code is working!

Code: Select all

;----------------------------
;   Input:
;   ESI = pointer to 0-terminated string
print:
   call getxy
   mov [cur_y],al
   mov [cur_x],ah

   xor cx,cx
   mov cl,ah
   shl cl,1

   mov bl,160
   mul bl

   add ax,cx

   mov edi,0b8000h
   add di,ax
.loop
   lodsb

   and al,al
   jz near .end

   cmp al,8
   je .tab

   cmp al,9
   je .back

   cmp al,10
   je near .new_line

   mov ah,7
   stosw

   cmp byte[cur_x],79
   je .new_line

   inc byte[cur_x]

   jmp .loop
.tab
   mov al,' '
   mov ah,7
   mov ecx,4
   rep stosw

   add byte[cur_x],4

   cmp byte[cur_x],79
   jng .loop

   mov al,[cur_x]
   sub al,4
   mov ah,79
   sub ah,al
   mov [cur_x],ah
   inc byte[cur_y]

   jmp .loop
.back
   cmp byte[cur_x],0
   jne .back1

   cmp byte[cur_y],0
   je .loop

   mov byte[cur_x],80
   dec byte[cur_y]
.back1
   dec byte[cur_x]
   sub edi,2
   mov al,' '
   mov ah,7
   stosw
   sub edi,2

   jmp .loop
.new_line
   cmp byte[cur_y],24
   je .scroll_down

   inc byte[cur_y]
   mov byte[cur_x],0
;----------------------------
   mov al,[cur_y]
;   mov ah,[cur_x]

;   xor cx,cx
;   mov cl,ah
;   shl cl,2

   mov bl,160
   mul bl

;   add ax,cx

   mov edi,0b8000h
   add di,ax
;----------------------------
   jmp .loop
.scroll_down
   push esi
   call scroll_down
   pop esi

   mov byte[cur_x],0

   mov edi,0b8f00h

   jmp .loop

.end
   mov al,[cur_y]
   mov ah,[cur_x]
   call gotoxy

   ret
;----------------------------
;   vars
cur_y   db 0
cur_x   db 0
;----------------------------

Re:ASM-print wont work right

Posted: Mon Feb 03, 2003 1:32 am
by Pype.Clicker
what i made for a fast PRINT in asm having all the tab, backspace, etc. features was to check %al against %20 (assuming ASC 00 .. ASC 1F were non-printable but rather command codes) and display immediately if above.

Code: Select all

    // prepare edi
.loop:
    lodsb
    cmp al,0x20
    jb .commands
    stosb
    jmp .loop

.commands:
    and eax,byte 0x1F
    jmp [print_commands+eax]

print_commands:
    dd end_of_message, ascii-display, ...
    dd do_tab (at 8th place), skip, do_CRLF, ...
this is known as "fast and slow path" optimization : you fasten the most common stuff and let the code be more complicated once you leave the default case.

Note that this code has a drawback that should be fixed: it doesn't check %edi remains in the 'console' area. This would cost 2 extra instruction.