flat assembler
Message board for the users of flat assembler.

Index > Main > drawstring algorithm problem

Author
Thread Post new topic Reply to topic
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 05 Mar 2011, 13:05
Hi,

im having trouble with a drawstring algorthim. Actually, its a array trouble.
For some reason ( that i hope you know Razz ), im not getting the correct address to the letter 'b':

Code:
; assuming edi = videobuffer
    drawstring:
        mov   esi,text
        xor   ecx,ecx
  .for0:push  ecx ; next char loop
        movzx ebx,byte[esi]
        test  ebx,ebx
        je    .done
        ;sub   ebx,'a'
        ;neg   ebx
        mov   ebx,[keys+0]  ; 0 = a, 4 = b ( theoricaly Sad )
                xor ecx,ecx
        .for1:  push ecx ; height loop
                        xor ecx,ecx
                .for2:  ; width loop
                        cmp dword[ebx],0
                        jz  @f
                        mov dword[edi+ecx*4],-1 ; set color
                     @@:add ebx,4
                        inc ecx
                        cmp ecx,8
                        jne .for2
                pop  ecx
                add  edi,1280*4
                inc ecx
                cmp ecx,8
                jne .for1
        pop  ecx
        inc  ecx
        mov  edi,[video_ptr]
        mov  ebx,ecx
        shl  ebx,3
        shl  ebx,2
        ;shl ebx,1
        add  edi,ebx
        inc  esi
        jmp  .for0
 .done: pop  ecx
        ret

align 4

  text db 'ababba',0

align 4

  keys dd a,b,0

align 4

a dd 0,0,0,0,0,0,0,0
  dd 0,0,0,0,0,0,0,0
  dd 0,0,1,1,1,0,0,0
  dd 0,0,0,0,0,1,0,0
  dd 0,0,1,1,1,1,0,0
  dd 0,1,0,0,0,1,0,0
  dd 0,1,0,0,0,1,0,0
  dd 0,0,1,1,1,0,1,0

b dd 0,1,1,0,0,0,0,0
  dd 0,0,1,0,0,0,0,0
  dd 0,0,1,0,0,0,0,0
  dd 0,0,1,0,1,1,1,0
  dd 0,0,1,1,0,0,0,1
  dd 0,0,1,0,0,0,0,1
  dd 0,0,1,1,0,0,0,1
  dd 0,1,1,0,1,1,1,0          


Description: output with mov ebx,[keys+4] - NO :(
Filesize: 4.51 KB
Viewed: 10566 Time(s)

bb.PNG


Description: output with mov ebx,[keys+0] - OK
Filesize: 3.37 KB
Viewed: 10566 Time(s)

aa.PNG



_________________
Sorry if bad english.
Post 05 Mar 2011, 13:05
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 05 Mar 2011, 13:36
> im not getting the correct address to the letter 'b'

Are you cutting off or corrupting the data maybe ? I can see the top line of your b Shocked

Some code quality issues to fix:

> add edi,1280*4

better:

add edi, [BPSL] ; Do NOT hardcode here

set BPSL and VRAM at same place.

----

> mov edi,[video_ptr]

This (VRAM) belongs to the beginning of your routine Wink

----

> push ecx
> pop ecx

this needs a clean-up

----

> shl ebx,3
> shl ebx,2
> ;shl ebx,1

better:

SHL EBX, 5 ; WtF ???

----

add comments: the 5 above, what is in the ECX pushed more above , ...

----

> a dd 0,0,0,0,0,0,0,0

You waste 32 bits to store just 1 bit Shocked

Reduce 32 to 8, later to 1 Wink
Post 05 Mar 2011, 13:36
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 05 Mar 2011, 14:22
Hi, DOS.
DOS386 wrote:
Are you cutting off or corrupting the data maybe ? I can see the top line of your b

i made a test, printing just a char, and the char a it print, but b it doens't. Maybe yes. How can i ensure that where i put my vars (db,dd,dw) is a safe place?
Quote:

better:

add edi, [BPSL] ; Do NOT hardcode here

set BPSL and VRAM at same place.

whats BPSL?

Quote:

> push ecx
> pop ecx

this needs a clean-up
what do you mean? Sad
Im just following revolution's statement: "make it works, then make it faster" (or something like that) Razz

Quote:
> a dd 0,0,0,0,0,0,0,0

You waste 32 bits to store just 1 bit Shocked

Reduce 32 to 8, later to 1 Wink

I still dont know how to work with values in the bit layer. I did dd just to get easy to me. Later i optimize. [rev's statem. =p]

_________________
Sorry if bad english.
Post 05 Mar 2011, 14:22
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 05 Mar 2011, 14:46
i've cleaned your source for mode 13h
now, it works.
Code:
        org 100h
        mov ax,13h
        int 10h
        push 0a000h
        pop fs
        mov esi,text
        call printf
        ret

db 'printf'
printf:
@@:
        movzx ecx,byte[esi]
        test ecx,ecx
        je @f
        call putc
        add [screen.x],8
        inc esi
        jmp @b
@@:
        ret
db 'putc'
putc:
        sub cl,'a'
        mov ecx,[keys+ecx*4]

        mov eax,[screen.x]
        mov ebx,[screen.y]
        mov dx,0707h
.loop:
        cmp byte[ecx],0
        je @f
        call putpixel
@@:
        inc ecx
        dec dl
        jl @f
        inc eax
        jmp .loop
@@:
        mov dl,7
        dec dh
        jl @f
        sub eax,7
        inc ebx
        jmp .loop
@@:
        ret
db 'putpixel'
putpixel:
        push eax ebx
        imul eax,[screen.bpp]
        imul ebx,[screen.bpsl]
        mov byte[fs:eax+ebx],red
        pop ebx eax
        ret
db 'screen'
screen:
.x       dd 0
.y       dd 0
.bpsl    dd 320      ;replace it with vesa byte per scan line
.bpp     dd 1        ;Byte per pixel, not bit per pixel
red=4

text db 'ababba',0

align 4
keys dd a,b

a db 0,0,0,0,0,0,0,0
  db 0,0,0,0,0,0,0,0
  db 0,0,1,1,1,0,0,0
  db 0,0,0,0,0,1,0,0
  db 0,0,1,1,1,1,0,0
  db 0,1,0,0,0,1,0,0
  db 0,1,0,0,0,1,0,0
  db 0,0,1,1,1,0,1,0

b db 0,1,1,0,0,0,0,0
  db 0,0,1,0,0,0,0,0
  db 0,0,1,0,0,0,0,0
  db 0,0,1,0,1,1,1,0
  db 0,0,1,1,0,0,0,1
  db 0,0,1,0,0,0,0,1
  db 0,0,1,1,0,0,0,1
  db 0,1,1,0,1,1,1,0     

and you just have to change fs to point to linear frame buffer, make bpsl = vesa bpsl, and bpp equal to byte per pixels from your vesa mode.

when you encounter many nested loops, prefer the use of calls.
Post 05 Mar 2011, 14:46
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 05 Mar 2011, 15:00
i'm using that in my OS, ed. its a lil bit different, isn't?


Description: here my entire code
Download
Filename: trunk.7z
Filesize: 5.01 KB
Downloaded: 289 Time(s)

Post 05 Mar 2011, 15:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 05 Mar 2011, 15:15
in an os, prefer the use of atomic functions.

printf>call putc
putc>call putpixel
putpixel>invoke screen structure
screen>contains screen datas


then, you will be able to share the properties of putpixel in every display functions.
then, you will be able to put a single char, just using putc.
i see in your "os" that you made drawchar, and just below, wrote drawstring.
and drawstring contains code to draw the char instead of using drawchar func, it is a misconception to do like this.
one more time i tell it to you, prefer the use of atomic functions, that will let you save memory, and develop many things on a simple way.

just look the code i gave you, printf (your drawchar) is only 8 instructions, and uses the putc atom
putc uses putpixel atom
and putpixel can be used to draw a single pixel without modification appart the color parameter that should be passed.

Code:
mov eax,X
mov ebx,Y
call putpixel
    

and you can use any calling convention to pass parameters.
Post 05 Mar 2011, 15:15
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 05 Mar 2011, 15:20
yea.. that 'drawchar' is just a try to draw a char and then i will put it inside drawstring. But neither drawchar is drawing correctly the letter 'b' (well, neither 'a' for awhile). Sad

[edit:]
i just checked my letter buffers ('a', 'b') is really being overrided. But why?
Post 05 Mar 2011, 15:20
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 05 Mar 2011, 20:59
i can't answer, i just modified your code to test it, and i can display b with your original line.

Code:
org 100h               ;;;;;;;;;;;;;;;;;;;
mov ax,13h             ;;;;;;;;;;;;;;;;;;;
int 10h                ;;;;;;;;;;;;;;;;;;;
push word 0a000h       ;;;;;;;;;;;;;;;;;;;
pop fs                 ;;;;;;;;;;;;;;;;;;;
mov edi,0              ;;;;;;;;;;;;;;;;;;;
call drawstring        ;;;;;;;;;;;;;;;;;;;
ret                    ;;;;;;;;;;;;;;;;;;;
video_ptr dd 0         ;;;;;;;;;;;;;;;;;;;

; assuming edi = videobuffer
    drawstring:
        mov   esi,text
        xor   ecx,ecx
  .for0:push  ecx ; next char loop
        movzx ebx,byte[esi]
        test  ebx,ebx
        je    .done
        ;sub   ebx,'a'
        ;neg   ebx
        mov   ebx,[keys+4]  ; 0 = a, 4 = b ( theoricaly Sad )
                xor ecx,ecx
        .for1:  push ecx ; height loop
                        xor ecx,ecx
                .for2:  ; width loop
                        cmp dword[ebx],0
                        jz  @f
;;;;;;;;;;;;;;;;;;;;;;;;;;;
                        mov byte[fs:edi+ecx],4 ; set color
                     @@:add ebx,4
                        inc ecx
                        cmp ecx,8
                        jne .for2
                pop  ecx
;;;;;;;;;;;;;;;;;;;
                add  edi,320
                inc ecx
                cmp ecx,8
                jne .for1
        pop  ecx
        inc  ecx
        mov  edi,[video_ptr]
        mov  ebx,ecx
        shl  ebx,3
;;;;;;;;;;;;
;        shl  ebx,2
        ;shl ebx,1
        add  edi,ebx
        inc  esi
        jmp  .for0
 .done: pop  ecx
        ret

align 4

  text db 'ababba',0

align 4

  keys dd a,b,0

align 4

a dd 0,0,0,0,0,0,0,0
  dd 0,0,0,0,0,0,0,0
  dd 0,0,1,1,1,0,0,0
  dd 0,0,0,0,0,1,0,0
  dd 0,0,1,1,1,1,0,0
  dd 0,1,0,0,0,1,0,0
  dd 0,1,0,0,0,1,0,0
  dd 0,0,1,1,1,0,1,0

b dd 0,1,1,0,0,0,0,0
  dd 0,0,1,0,0,0,0,0
  dd 0,0,1,0,0,0,0,0
  dd 0,0,1,0,1,1,1,0
  dd 0,0,1,1,0,0,0,1
  dd 0,0,1,0,0,0,0,1
  dd 0,0,1,1,0,0,0,1
  dd 0,1,1,0,1,1,1,0
    

lines i just adapted are marked by ;;;;;;;;;;;;;;;;;

it work for me, then, i cannot tell you why it don't work for you.

maybe the rest of the code of the os is the source of the problem. ('b' overwritten)


Description:
Filesize: 2.87 KB
Viewed: 10502 Time(s)

testteehee.png


Post 05 Mar 2011, 20:59
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 06 Mar 2011, 07:35
off topic: It is really weird way to use dword for every pixel in the font. If normally one pixel uses 1bit - then you are using 32 times more memory than is needed. Assembly programs should be small fast and beautiful.

Regards
Post 06 Mar 2011, 07:35
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 06 Mar 2011, 09:31
Quote:
it work for me


Horrible MEVDM and even en francais Sad

JohnFound wrote:
off topic: It is really weird way to use dword for every pixel in the font. If normally one pixel uses 1bit


already pointed, see above Smile
Post 06 Mar 2011, 09:31
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 06 Mar 2011, 12:16
Quote:
Horrible MEVDM and even en francais

just made the original code works under DOS.

i don't see the horrible problem.
Post 06 Mar 2011, 12:16
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 07 Mar 2011, 18:05
Guys it is working!!! Laughing Laughing Laughing

The 'b' were overriden bc i do not loaded the 3º sector Embarassed

Thank you very much!!!! Very Happy
Post 07 Mar 2011, 18:05
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 08 Mar 2011, 03:22
Teehee wrote:
whats BPSL?


WtF ??? Did you write the code ? You don't know why you did put in the "1280*4" ? Confused

Teehee wrote:
Guys it is working!!!


COOL Smile

Quote:
The 'b' were overriden bc i do not loaded the 3º sector


As suspected Wink
Post 08 Mar 2011, 03:22
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 08 Mar 2011, 11:09
DOS386 wrote:
WtF ??? Did you write the code ? You don't know why you did put in the "1280*4" ? Confused

first time i read i didn't assign the bpsl with bytes per scan line Razz

_________________
Sorry if bad english.
Post 08 Mar 2011, 11:09
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 09 Mar 2011, 22:20
DOS386 wrote:
Reduce 32 to 8, later to 1 Wink

Can anyone give an example of how to work with a font bit-to-bit?

if i let my font in 'db', i will lost ~16kb ;/

_________________
Sorry if bad english.
Post 09 Mar 2011, 22:20
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 09 Mar 2011, 22:31
http://board.flatassembler.net/topic.php?p=63088#63088

it is not the last state of this font, but it is the only one that is still loaded here, and not for fool.

to use it, just include it in your source, and call txt, with esi pointing to the string and (txt.x, txt.y) set to the coordinates you want.

there is a version somewhere for vesa, search for "fbrowser contest" in os construction.
Post 09 Mar 2011, 22:31
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 09 Mar 2011, 22:35
thanks, i will check.
Post 09 Mar 2011, 22:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.