Difference between revisions of "Code Generation/Example 3"

From Wiki**3

< Code Generation
(Postfix Code)
(Postfix Code)
Line 27: Line 27:
 
;; i = dim-2
 
;; i = dim-2
 
LOCAL 12
 
LOCAL 12
LOAD
+
LDINT
 
INT 2
 
INT 2
 
SUB
 
SUB
DUP
+
DUP32
 
LOCAL -8
 
LOCAL -8
STORE
+
STINT
 
TRASH 4
 
TRASH 4
  
 
;; res = x + dim - 1
 
;; res = x + dim - 1
 
LOCAL 8
 
LOCAL 8
LOAD
+
LDINT
 
LOCAL 12
 
LOCAL 12
LOAD
+
LDINT
 
INT 4
 
INT 4
 
MUL
 
MUL
Line 47: Line 47:
 
MUL
 
MUL
 
SUB
 
SUB
DUP
+
DUP32
 
LOCAL -4
 
LOCAL -4
STORE
+
STINT
 
TRASH 4
 
TRASH 4
  
Line 56: Line 56:
 
LABEL for
 
LABEL for
 
LOCAL -8
 
LOCAL -8
LOAD
+
LDINT
 
INT 0
 
INT 0
 
GE
 
GE
Line 65: Line 65:
 
;; if test
 
;; if test
 
LOCAL 8
 
LOCAL 8
LOAD ; x
+
LDINT ; x
 
LOCAL -8
 
LOCAL -8
LOAD ; i
+
LDINT; i
 
INT 4
 
INT 4
 
MUL
 
MUL
 
ADD ; x+i
 
ADD ; x+i
LOAD ; x[i] = *(x+i)
+
LDINT ; x[i] = *(x+i)
  
 
LOCAL -4
 
LOCAL -4
LOAD ; res
+
LDINT ; res
LOAD ; *res
+
LDINT ; *res
  
 
GT
 
GT
Line 82: Line 82:
 
;; if block
 
;; if block
 
LOCAL 8
 
LOCAL 8
LOAD ; x
+
LDINT ; x
 
LOCAL -8
 
LOCAL -8
LOAD ; i
+
LDINT ; i
 
INT 4
 
INT 4
 
MUL
 
MUL
 
ADD ; x+i = &x[i]
 
ADD ; x+i = &x[i]
DUP
+
DUP32
 
LOCAL -4
 
LOCAL -4
STORE ; res = &x[i]
+
STINT ; res = &x[i]
 
TRASH 4
 
TRASH 4
  
Line 100: Line 100:
 
LABEL forincr
 
LABEL forincr
 
LOCAL -8
 
LOCAL -8
LOAD ; i
+
LDINT ; i
DUP
+
DUP32
 
INT 1
 
INT 1
 
SUB
 
SUB
 
LOCAL -8
 
LOCAL -8
STORE
+
STINT
 
TRASH 4
 
TRASH 4
  
Line 116: Line 116:
 
;; return
 
;; return
 
LOCAL -4
 
LOCAL -4
LOAD ; res
+
LDINT ; res
  
POP
+
STFVAL32
 
LEAVE
 
LEAVE
 
RET
 
RET

Revision as of 00:30, 2 May 2018

The Original Code

Consider the following C function:

<c> int *click(int *x, int dim) {

 int *res, i;
 for (i = dim-2, res = x+dim-1; i >= 0; i--)
   if (x[i] > *res) res = &x[i];
 return res;

} </c>

Postfix Code

The Postfix code for the above function is as follows:

Postfix code
{{{2}}}

Compiling and Running

To compile the Postfix code directly, pf2asm can be used:

  • pf2asm click.pf
  • yasm -felf click.asm