flat assembler
Message board for the users of flat assembler.

Index > Main > [stack issues] What's wrong with this code ?

Author
Thread Post new topic Reply to topic
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 29 Dec 2011, 14:17
Hi.
I recently found a C program that draws pixels on the screen.
I wrote (converted) that program with FASM but it has some errors and causes CPU exceptions but I don't know what is the problem. please check.
Thanks Smile


Description: Parameters are pushed to stack left to right.
Download
Filename: pixel.asm
Filesize: 1.07 KB
Downloaded: 317 Time(s)

Post 29 Dec 2011, 14:17
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20418
Location: In your JS exploiting you and your system
revolution 29 Dec 2011, 14:31
When you use call the CPU will push the return address on the stack so you must account for that when trying to read the parameters back off the stack.
Code:
             push %                  ; X = current loop counter
          push %                  ; Y = current loop counter
          push %                  ; color = current loop counter
              call plot_pixel_slow    ; call Draw function --->(CPU pushed the return address)
;...
plot_pixel_slow:
 pop ax                  ; Move color to al --->(pops the return address)
 mov ah, WRITE_DOT       ; Move 0x00 to ah
   pop dx                  ; Y position of pixel --->(pops the color)
       pop cx                  ; X position of pixel --->(pops the Y value)
     int VIDEO_INT           ; Call interrupt function
   ret ;--->ret returns to the X value!    
Post 29 Dec 2011, 14:31
View user's profile Send private message Visit poster's website Reply with quote
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 29 Dec 2011, 16:45
I didn't understand. You mean I use two pop instructions to get function parameters, because first is waste?
Code:
push %                  ; color = current loop counter
                call plot_pixel_slow    ; call Draw function --->(CPU pushed the return address)
;...
plot_pixel_slow:
        pop ax                  ; Move color to al --->(pops the return address)
        pop ax                  ; <-- You mean this?
        mov ah, WRITE_DOT       ; Move 0x00 to ah
        pop dx                  ; Y position of pixel --->(pops the color)     
Post 29 Dec 2011, 16:45
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20418
Location: In your JS exploiting you and your system
revolution 29 Dec 2011, 17:17
You have no choice, the CPU will push the return address so you must find a way to skip that to access your parameters. Usually the BP register can be used with the ret n instruction.
Code:
     push    %               ; X = current loop counter
  push    %               ; Y = current loop counter
  push    %               ; color = current loop counter
      call    plot_pixel_slow ; call Draw function --->(CPU pushes the return address)
;...
plot_pixel_slow:
 push    bp              ; save bp
   mov     bp,sp
       mov     ax,[bp+4]       ; Move color to al
  mov     ah, WRITE_DOT
       mov     dx,[bp+6]       ; Y position of pixel --->(pops the color)
       mov     cx,[bp+8]       ; X position of pixel --->(pops the Y value)
     int     VIDEO_INT
   leave
       ret     6               ; pop all parameters off the stack and return    
Post 29 Dec 2011, 17:17
View user's profile Send private message Visit poster's website Reply with quote
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 29 Dec 2011, 19:14
Thank you, but would you explain your code please? why you add 4 with bp for the first parameter?
BTW, the screen closes fast. How can I stop it in a period of time or wait for key press?
Thanks Very Happy
Post 29 Dec 2011, 19:14
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20418
Location: In your JS exploiting you and your system
revolution 29 Dec 2011, 19:25
majidkamali1370 wrote:
why you add 4 with bp for the first parameter?
Because there are two other things on the stack 1) the return address and 2) the saved BP register value.
Post 29 Dec 2011, 19:25
View user's profile Send private message Visit poster's website Reply with quote
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 29 Dec 2011, 19:37
Thank you. Laughing
Post 29 Dec 2011, 19:37
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number 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.