Code Generation/Example 2: Difference between revisions

From Wiki**3

Root (talk | contribs)
Root (talk | contribs)
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
<source lang="c">
<source lang="c">
const char *a = "batata";
const char *a = "batata";
int main {
int main() {
   prints(a);
   prints(a);
   return 0;
   return 0;
Line 20: Line 20:
ALIGN
ALIGN
LABEL _L123  ;; automatic label
LABEL _L123  ;; automatic label
STR "batata"
SSTRING "batata"
;--- declaring the global variable "a"
;--- declaring the global variable "a"
DATA
DATA
ALIGN
ALIGN
GLOBAL a, OBJ
LABEL a
LABEL a
SADDR  _L123  ;; automatic label
SADDR  _L123  ;; automatic label
Line 30: Line 31:
TEXT
TEXT
ALIGN
ALIGN
GLOBL _main, FUNC
GLOBAL _main, FUNC
LABEL _main
LABEL _main
ENTER 0
ENTER 0
Line 36: Line 37:
ADDR a
ADDR a
LDINT
LDINT
EXTRN prints
EXTERN prints
CALL prints
CALL prints
TRASH 4
TRASH 4

Latest revision as of 15:56, 6 May 2020

The Original Code

Consider the following C function:

const char *a = "batata";
int main() {
  prints(a);
  return 0;
}

Postfix Code

The Postfix code for the above function is as follows:

Postfix code
;--- declaring the string literal
RODATA
ALIGN
LABEL _L123  ;; automatic label
SSTRING "batata"
;--- declaring the global variable "a"
DATA
ALIGN
GLOBAL a, OBJ
LABEL a
SADDR  _L123  ;; automatic label

;--- this is the main function (note that "s9" translates to RTS's "_main")
TEXT
ALIGN
GLOBAL _main, FUNC
LABEL _main
ENTER 0

ADDR a
LDINT
EXTERN prints
CALL prints
TRASH 4

INT 0
STFVAL32
LEAVE
RET

Compiling and Running

To test the second program directly, pf2asm can be used:

pf2asm batata.pf
yasm -felf batata.asm
ld -o batata batata.o -lrts