flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > 'out' macro

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Hello,
I have made my own 'out' macro. It supports the six types of 'out'.

Code:
macro out port,data
{
     if port < 256 & data < 256                         ;port8 / data8
                mov     al,data
                out     port,al
else if port < 256 & data > 255 & data < 65536          ;port8 / data16
                mov     ax,data
                out     port,ax
...
    


1. I would like to know if it can be optimized. For example, is there a better way to detect if the input is a byte or word or dword?
2. I also have a problem. If I do not use an immediate value (like 'out 0x60,0xaa'), it does not work. For example, if I do a 'out 0x60,al', I will get an 'error: invalid value'. Why is that?

Thank you in advance.
Post 30 Oct 2006, 11:52
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
ManOfSteel wrote:
if it can be optimized
Check for like this:
Code:
if (data and 0xffffffffffffff00) = 0 ;byte
;..
elseif (data and 0xffffffffffff0000) = 0 ;word
;..
elseif (data and 0xffffffff00000000) = 0 ;dword
;..
else ;qword
;..
end if    


ManOfSteel wrote:
if I do a 'out 0x60,al', I will get an 'error: invalid value'. Why is that?
fasm will do textual replacement of macro parameters, with "out 0x60,al": "data" is replaced by "al" so the first line is assembled like this:
Code:
if 0x60 < 256 & al < 256    
You need to check if the input parameter "data" is "al" before you try to use the parameter with a numeric comparison operator.
Post 30 Oct 2006, 12:18
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
What if you want to send 16-bit value like 0000h?
Post 30 Oct 2006, 13:08
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
i suggest syntax like:
Code:
out 14h, byte 18
out 14h, word 0    

and use "match" to check type

and later
Code:
out 14h, <"abcd", 13,10, "aa">    
Wink
Post 30 Oct 2006, 13:26
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
revolution,
Quote:
You need to check if the input parameter "data" is "al" before you try to use the parameter with a numeric comparison operator.

Well, that's the point! How can I do that? How can I detect if 'data' is a register or an immediate value.

Tomasz Grysztar,
Quote:
What if you want to send 16-bit value like 0000h?

As I said, it supports the six types of 'out' shown in Intel manuals (port8/data8, port8/data16, port8/data32, port16/data8, port16/data16, port16/data32). I did not post them all, because it is not the goal of the thread (and it is easy to complete, anyway). My only problem is that it does not accept registers.
Post 31 Oct 2006, 07:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
ManOfSteel wrote:
Well, that's the point! How can I do that? How can I detect if 'data' is a register or an immediate value.
vid already gave you the hint:
vid wrote:
and use "match" to check type
Post 31 Oct 2006, 10:41
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
How can I detect if 'data' is a register or an immediate value.


assembly time "if" directive:
Code:
macro jesus arg {
if arg in <al, bl, cl, dl, ah, dh, ch, dh>
  ;8bit register
else if arg in <ax, bx, cx, dx, bp, sp, si, di>
  ;16bit register
else if arg in <eax, ebx, ecx, edx, ebp, esp, esi, edi>
  ;32bit register
end if
}    
Post 31 Oct 2006, 11:44
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
ManOfSteel wrote:
Quote:
What if you want to send 16-bit value like 0000h?

As I said, it supports the six types of 'out' shown in Intel manuals (port8/data8, port8/data16, port8/data32, port16/data8, port16/data16, port16/data32). I did not post them all, because it is not the goal of the thread (and it is easy to complete, anyway). My only problem is that it does not accept registers.

Perhaps you misunderstood. I was refering to the fact, that you detect 16-bit value by checking whether it is larger than 255 and smaller than 65536. However you may still need to send a 16-bit value smaller than 256. Thus the vid's suggestion to use syntax like "byte ..."/"word ...".
Post 01 Nov 2006, 18:16
View user's profile Send private message Visit poster's website Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Hello,
now it does six 'if' to know whether the data is in the accumulator and the port is within the right boundaries and then six other 'if' to know if both the port and the data are within the right boundaries (when working with immediate values only).
In the first six 'if', I have removed the 'mov {register},data' so there is no redundance.
The code is quite long now, but it does what it is supposed to do.

It works with any of these:
Code:
out 0x60,al
out 0x60,ax
out 0x60,eax
out 0x512,al
out 0x512,ax
out 0x512,eax
out 0x60,2
out 0x60,512
out 0x60,2097152
out 0x512,2
out 0x512,512
out 0x512,2097152
    


Just one more thing (by curiosity), could you tell me how could it have been done with the match directive.



Tomasz Grysztar,
I will do what was suggested by vid, but I have replaced all the out instructions by the macro and I have not encountered any problem (so far). The whole OS was perfectly assembled and runs happily.



Thanks everyone for all the help!
Post 02 Nov 2006, 07:25
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Nay! I cannot figure this out. I tried to do it like vid said, but I am not getting any positive results. I really need some deeper explanation with this one (match directive used to detect parameter size). A small piece of code about "match" would be perfect.
Post 03 Nov 2006, 11:53
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Code:
macro out port, val {
match =byte val, val \{
  mov al, val
  out port, al  
  \}
match =word val, val \{
  mov al, val
  out port, al  
  \}
... etc
}    


this is just how match works, you need also to check for case when nothing matched and throw error, or try to use original "out" instruction:
Code:
macro out port, val {
done equ 0
match =byte val, val \{
  mov al, val
  out port, al  
  done equ 1
  \}
match =word val, val \{
  mov al, val
  out port, al  
  done equ 1
  \}
match =0, done \{
  out port, val
\}
... etc
}    


read manual about usage of match
Post 03 Nov 2006, 13:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
How is it supposed to work? Using for example 'out 10,45' does precisely nothing when I remove the last match and otherwise throws an 'invalid operand' error which is quite weird since it has the same syntax as the original 'out' instruction. Also, passing any random garbage will have the same result.
Quote:

flat assembler version 1.67.14 (38212 kilobytes memory)
1 passes, 0 bytes.


I upgraded from fasm 1.66 yesterday thinking it was just a bug. Apparently it was not.

There is something wrong or missing here.
Post 04 Nov 2006, 21:55
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
because you don't know if you want to pass byte 45, or word 45 (byte 45 and byte 0), or dword 45 (byte 45 and 3 bytes 0) etc...

i am also not sure about what can be OUT instruction's arguments. You cannot do "out 10, 45" directly?
Post 05 Nov 2006, 16:42
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
vid wrote:
i am also not sure about what can be OUT instruction's arguments. You cannot do "out 10, 45" directly?

http://flatassembler.net/docs.php?article=manual#2.1.7
Wink
Post 05 Nov 2006, 16:44
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
i know, i'm just lazy
Post 05 Nov 2006, 16:50
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
That's what I thought.
Post 05 Nov 2006, 16:51
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
so, last match should be:
Code:
match =0, done \{ 
  mov al, val
  out port, al 
\}    
.
Post 05 Nov 2006, 16:52
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Well, it still does not work, even with a 'byte' or 'word' before the value. And I think I realized why. It seems fasm thinks 'byte val' or 'word val' are two separate parameters.
I tried the following.

Code:
macro out port,val
{
done equ 0
match =byte val,val val
  \{
  ;mov al,val
  ;out port,al
  done equ 1 
  display 'byte'
  \}
match =word val,val val
  \{
  ;mov al,val
  ;out port,al
  done equ 1 
  display 'word'
  \}
match =0,done
  \{display 'error'\}
}
    

out 0x60,byte 10

It displays 'byte' when using 'byte 10' and 'word' when using 'word 10', but once I uncomment the actual 'out' instructions, it does not work anymore.

Anyway, I have added a new parameter just for the size and reprogrammed the whole thing.


Code:
macro   out port,size,data
{

...
if data in <al,ax,eax> & port < 256
;port is a byte and data is in a register
        match   = byte,size
;...
        match   = word,size
;...
        match   = dword,size
;etc

else if port < 256
;same as above when data is an immediate
;etc

else if data in <al,ax,eax> & port > 255
;same as above when port is a word and data is in a register
;etc

else if port > 255
;same as above when port is a word and data is an immediate
;etc

end if
;...

}
    


It is used like this: out 0x60,dword,10 , and it works well.

I think I am done with it this time, but of course if someone has any comment on the code or explanation on why it did not work with 'byte xxxx' and 'word xxxx', s/he is welcome.

And thank you to everyone who helped me.
Post 07 Nov 2006, 09:08
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7724
Location: Kraków, Poland
Tomasz Grysztar
The correct macro should be like:
Code:
macro out port,val
{
done equ 0
match =byte v,val
  \{
  mov al,v
  out port,al
  done equ 1 
  \}
match =word v,val
  \{
  mov ax,v
  out port,ax
  done equ 1 
  \}
match =dword v,val
  \{
  mov eax,v
  out port,eax
  done equ 1 
  \}
match =0,done
  \{display 'error'\}
}    
Post 07 Nov 2006, 09:28
View user's profile Send private message Visit poster's website Reply with quote
dark_teo



Joined: 26 Jul 2005
Posts: 5
dark_teo
how out (port,val)

for example:
out (3f8h,byte 20h)
out (bx,al)
out (3f8h,bx)


????
Post 14 Nov 2006, 23:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2020, Tomasz Grysztar.

Powered by rwasa.