flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > Understanding IRP, IRPS, IRPV
Author
m3ntal

Joined: 08 Dec 2013
Posts: 296
m3ntal 15 Jul 2014, 21:04
A small example I found on my PC and thought might be helpful. To view result, click Compile or press Ctrl+F9.

Code:
```; EXAMPLE: IRP, IRPS, IRPV

; irp/s iterates through a list of symbols
; separated with commas (irp) or spaces (irps)

; display ABC.123.XYZ using irp/s

irp x,\
'A','B','C','.' {
display x
}

irps x,\
'1' '2' '3' '.' {
display x
}

e equ 'X','Y','Z'

irp x, e { display x }

restore e

macro display.nl { display 0Dh, 0Ah }

display.nl

; xor eax/ecx/edx

irps x,\
'a' 'c' 'd' {
r equ 'e'#x#'x'
display 'xor ', r, ',', r
display.nl
restore r
}

; create a list then iterate through it.
; first match is needed to convert/expand
; the single "name token" e into list a,b,c

e equ 1,2,3      ; create list

n=0              ; count # items...
match a, e {     ; convert/expand
irp x, a \{     ; iterate through list
n=n+1
\}
}

display '#',\    ; display #
n+'0', ':', ' '

match a, e {     ; convert/expand
irp x, a \{     ; iterate through list
display \      ; display all items
x+'0', ' '
\}
}
display.nl

macro display.list [e] {
common
match a, e \{  ; convert/expand
irps x, a \\{ ; iterate through list
display \    ; display all items
x+'0', ' '
\\}
\}
}

display.list 4 5 6 7
display.nl

restore e ; finished

; irpv iterates through the stacked list
; of equate definitions. if the equate is
; undefined, irpv will not be processed

e equ 7       ; create equate stack
e equ 6       ; of digits
e equ 5

n=0           ; count # equates
irpv x, e {   ; iterate through stack
n=n+1
}

display \     ; display # equates
'#', n+'0',\
':', ' '

irpv x, e {   ; iterate through stack
display \    ; display all digits
x+'0', ' '
}
display.nl

irpv x, e {   ; restore equates
restore e
}             ; now, e is undefined

; ensure 'e' was properly restored.
; to see this message, comment
; "; restore e" above

irpv x, e {
'Error: Not restored'
}

; do this inside of a macro...

macro test1 [p] {
common            ; once...
local list
list equ
match e, p \{    ; collect and
restore list    ; store parameters
irps x, e \\{   ; in equate stack
list equ x
\\}
\}
irpv x, list \{  ; iterate through
display \       ; equate stack
x+'0', ' '     ; display digits
\}
display.nl
irpv x, list \{  ; restore each one
restore list
\}
}

test1 4 3 2 1

; OUTPUT:

; ABC.123.XYZ
; xor eax,eax
; xor ecx,ecx
; xor edx,edx
; #3: 1 2 3
; 4 5 6 7
; #3: 7 6 5
; 4 3 2 1    ```
15 Jul 2014, 21:04
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 16 Jul 2014, 02:00
m3ntal,

Are you sure that in
Code:
```e equ 'X','Y','Z'

irp x, e { display x }    ```
irp iterates? It expands to display e, then e is substituted with 'X','Y','Z' as usual. Later you're using match to expand symbolic constants, why not here?
16 Jul 2014, 02:00
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 19250
revolution 16 Jul 2014, 02:08
Perhaps a better way to show if expansion is active or not is to use:
Code:
`irp x, e { display x,'-' }    `
If you see three dashes then you know that IRP iterated, else you'll see only one dash and know that e was not expanded. For a newbie this kind of thing would be informative.
16 Jul 2014, 02:08
m3ntal

Joined: 08 Dec 2013
Posts: 296
m3ntal 16 Jul 2014, 03:48
baldr: I thought irp/x was an abbreviation for iterate/repeat. Match is not needed in that case.

Seems like you're always trying to discredit me, but what do you contribute? You have knowledge of macros? Please write some tutorials, make a web site, do something productive and you'll feel good about yourself.

Thanks for the suggestion, revolution.
16 Jul 2014, 03:48
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 16 Jul 2014, 07:25
m3ntal wrote:
Seems like you're always trying to discredit me…
Please don't be overly paranoid. Was my comment rude/embarrassing/defamatory? If you think so, please accept my sincere apologies.

OTOH, I thought it deserves to mention that irp* instant macros don't substitute symbolic constants (actually you'd used match several times later in the source exactly to overcome this).

Hope this helps.
16 Jul 2014, 07:25
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum