flat assembler
Message board for the users of flat assembler.

Index > DOS > How can I make this code look better?

Author
Thread Post new topic Reply to topic
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 13 Jun 2008, 14:16
My code writing is horrible, how can I make this look better?
Also, is there any way to optimise this code?
Code:
org 100h

mov ax,13h
int 10h

push 0a000h
pop es

mov ax,0h
mov bx,16
mov di,6500

start:
mov cx,16
iloop:
stosb
stosb
add di,318
stosb
stosb
sub di,320
inc ax
loop iloop
add di,608
dec bx
cmp bx,0
jne start

key:
in ax,60h
dec ax
jnz key
mov ax,03h
int 10h
ret
    

_________________
----> * <---- My star, won HERE
Post 13 Jun 2008, 14:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20333
Location: In your JS exploiting you and your system
revolution 13 Jun 2008, 14:52
Not exhaustive but just some ideas ...
  • Use symbolic constants.
  • Put in comments.
  • Use tab alignment.
  • Put some header text to say what the intended purpose is.
Post 13 Jun 2008, 14:52
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 13 Jun 2008, 18:38
This any better?

Code:
;Shows all colors in palette in 2x2 blocks

        org 100h      ;make it .com

        mov ax,13h    ;video mode 320x200 256 colors
        int 10h       ;^^^

        push 0a000h   ;put screen offset into es
        pop es        ;^^^

        xor ax,ax     ;zero out ax
        mov bx,10h    ;how many lines vertically will it draw?
        mov di,6990h  ;make it start drawing at a place so the thing will be centered

oloop:
        mov cx,10h    ;how many pixels horizantly

iloop:
        stosb         ;plot color in 2x2 block
        stosb         ;^^^
        add di,13eh   ;^^^
        stosb         ;^^^
        stosb         ;^^^
        sub di,140h   ;^^^
        inc ax        ;increase color
        loop iloop    ;loop 16 times
        add di,260h   ;go to next line down on screen
        dec bx
        cmp bx,0h     ;have we drawn all 16 lines?
        jne oloop     ;no? continue drawing.....if yes, then keep going

key:
        in ax,60h     ;get key press
        dec ax        ;
        jnz key       ;was it esc? if not, go back to key checker
        mov ax,03h    ;return video mode
        int 10h       ;^^^
        ret           ;exit
    

_________________
----> * <---- My star, won HERE
Post 13 Jun 2008, 18:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20333
Location: In your JS exploiting you and your system
revolution 13 Jun 2008, 19:04
wrote:
This any better?
Very nice. Considerably easier to read.

Think about using x_size=320, y_size=200, block_height=2, etc.

Perhaps the constant 6990h could be converted to the calculation i.e.
Code:
start_corner = 84*x_size+(x_size-block_width*block_count_x)/2    
Post 13 Jun 2008, 19:04
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4037
Location: vpcmpistri
bitRAKE 14 Jun 2008, 01:16
This is just another way to code that same thing:
Code:
;Shows all colors in palette in 2x2 blocks

        org 100h      ;make it .com

        mov ax,13h    ;video mode 320x200 256 colors
        int 10h       ;^^^

        push 0a000h   ;put screen offset into es
        pop es        ;^^^

        ;make it start drawing at a place so the thing will be centered
        mov di,6990h-120h

        mov cx,10h    ;how many color columns horizontally

        xor ax,ax     ;start with color zero
.0:     add di,120h   ;go to next line down on screen

        push cx
        push cx
        ; first line
.1a:    stosw
        add ax,0101h  ;increase color
        loop .1a

        add di,120h   ;go to next line down on screen
        sub ax,1010h  ;back-up to starting line color

        pop cx
        ; second line
.1b:    stosw
        add ax,0101h  ;increase color
        loop .1b
        pop cx

        jnc .0
key:
        in ax,60h     ;get key press
        dec ax        ;
        jnz key       ;was it esc? if not, go back to key checker

        mov ax,03h    ;return video mode
        int 10h       ;^^^

        ret           ;exit    

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 14 Jun 2008, 01:16
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 14 Jun 2008, 02:35
neat. I was gonna use stosw but wasn't thinking right and it was annoying me.
mine is 1 byte smaller Smile

_________________
----> * <---- My star, won HERE
Post 14 Jun 2008, 02:35
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4037
Location: vpcmpistri
bitRAKE 14 Jun 2008, 04:08
windwakr wrote:
mine is 1 byte smaller Smile
(click)
Oh, don't go there - I'll have to break out the can of whoop ass, lol!

(down to 45 bytes so far...)

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 14 Jun 2008, 04:08
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4332
Location: Now
edfed 14 Jun 2008, 07:03
Code:
        mov ax,0c00h  ; clear keyboard buffer
        int 21h          
        in AL,60h         ; scancode port is a byte, not a word.
    
Post 14 Jun 2008, 07:03
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 14 Jun 2008, 11:33
edfed wrote:

> ; scancode port is a byte, not a word.

Exactly ... same bug in both threads ... also see the other one for shot Wink
Post 14 Jun 2008, 11:33
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 14 Jun 2008, 12:30
Thanks for showing me all the errors in my code.
Post 14 Jun 2008, 12:30
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4037
Location: vpcmpistri
bitRAKE 15 Jun 2008, 07:32
It is fairly common to assume register values on entry to a COM program - especially when it comes to size optimization. The Hugi competition use the following values:
Code:
;          AX BX = 0000
;             CX = 00FF
; DX CS DS ES SS = .... (same unknown value)
;             SI = 0100
;             DI = FFFE
;             BP = 09..
;             SP = FFFE
;             IP = 0100    
Couldn't sleep so I re-examined the 45 bytes and whittled it down to 41 (or 42 for those bothered by IN AX,60h - we all know 42 is the answer).
Code:
        org $100
        mov al,$13
        int $10
        les dx,[bx]
        xchg ax,bx ; salc
        mov di,$6880
.0:     add di,$0120
        mov cl,$10
.1:     stosb
        stosb
        inc ax
        loop .1
        add ax,$00F0
        lahf
        jc .0
        add al,$10
        jne .0
key:    in ax,60h
        dec ax
;more compatible? code, another byte
;        in al,$60
;        dec al
        jnz key
        mov al,03h
        int 10h
        ret    
Tried several exotic variants: trying to generate the constants or branching efficiently. Even tried working backward and two interlaced line passes; but nothing looks promising, yet. The ever present sense I'm missing something rather simple hangs.

I'd like to just reformulate the problem to fit easier code. Laughing Maybe, just produce a centered 16x16 grid of similar rectangles displaying all the colors? Adds quite a bit of freedom, imho.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 15 Jun 2008, 07:32
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20333
Location: In your JS exploiting you and your system
revolution 15 Jun 2008, 07:50
bitRAKE: The OP isn't looking to make the code small (and harder to understand), the OP wanted to make the code readable.
Post 15 Jun 2008, 07:50
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4037
Location: vpcmpistri
bitRAKE 16 Jun 2008, 02:12
Thought he already reached that goal?

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 16 Jun 2008, 02:12
View user's profile Send private message Visit poster's website Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP 16 Jun 2008, 02:23
About the indenting, I usually do this:
- Align all instructions to 16 spaces from left
- Align beginning of operands 8 spaces after (3-letters + 5 spaces)
- Keep a space after commas between operands

About labels, I do:
- Keep major code labels along left hand of editor
- Preceed common, minor labels with '@@', six spaces from left
- Keep a line before labels
- Keep labels on seperate lines (no 'Label: instruction' )
- Keep a label operand two spaces before usual location (see below)

Amounts to:
Code:

      ; Set up an SEH for reading exceptions later

                lea     eax, [ebp+offset Exception]
                push    eax                ; SEH handler will just
                push    dword [fs:0]       ; exit on exception
                mov     [fs:0], esp

      ; Now find Kernel32 using return address on stack

                mov     eax, [esp+2Ch]
                and     eax, 0FFFFF000h
                mov     ecx, 100h ; ecx is a counter for loop

      @@FindKernelHeader:
                sub     eax, 1000h       ; loop backwards until you find
                cmp     word [eax], 'MZ' ; what looks like a header
                jz    @@CheckKernelForPESignature ; check for PE
                loop  @@FindKernelHeader         ; try again
                jmp     ExitTANGAR              ; couldn't find it...

      @@CheckKernelForPESignature:
                mov     ebx, [eax+3Ch]   ; PE signature RVA
                add     ebx, eax         ; PE signature address
                cmp     word [ebx], 'PE' ; check if it's PE format
                jnz   @@FindKernelHeader

      ; And then search it's export table to find GetProcAddress

      @@KernelFound:
                mov     ebx, [ebx+78h] ; export table RVA
                add     ebx, eax       ; export table address
...
    

And you get it... I believe it's a nice structure to it.
Post 16 Jun 2008, 02:23
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 16 Jun 2008, 10:07
bitRAKE wrote:
It is fairly common to assume register values on entry to a COM program - especially when it comes to size optimization.


Common ... maybe ... good idea : NO Sad

Code:
key:    in ax,60h
        dec ax
;more compatible? code, another byte
;        in al,$60
;        dec al
    


AX doesn't work at all here ... you could just use

Code:
   cli
   hlt
    

or
Code:
   int3
    


for same effect Sad

> how can I make this look better?

- Place a (C) or (CL), date, author, hint about complier used (FASM), what it is supposed to do, ...
- Use comments (here Tomasz is not the best example Neutral )
- Align instructions and operands
- Write 0 as "0" , not "0h"
- FASM also allows to prefix hex with a $

Just see some of my examples: http://board.flatassembler.net/topic.php?t=8670

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 16 Jun 2008, 10:07
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.