Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 5 Suppose the target assembly language for a compiler has these five in

ID: 3664904 • Letter: E

Question

Exercise 5 Suppose the target assembly language for a compiler has these five instructions for integers:

push address addsubmul

pop address

In these instructions, an address is the name of a static variable (whose actual ad- dress will be filled in by the loader). The machine maintains a stack of integers, which can grow to any size. The push instruction pushes the integer from the giv- en memory address to the top of the stack. The add instruction adds the top integer on the stack to the next-from-the-top integer, pops both off, and pushes the result onto the stack. The sub instruction subtracts the top integer on the stack from the next-from-the-top integer, pops both off, and pushes the result onto the stack. The mul instruction multiplies the top integer on the stack by the next-from-the-top in- teger, pops both off, and pushes the result onto the stack. The pop instruction pops an integer off the stack and stores it at the given memory address. So, for example, the compiler might translate the assignment result := offset+(width*n) into this:

       push offset

       push width

       push n

       mul

       add

       pop result

Using this assembly language, give translations of the assignment statements in Exercise 4. Use as few instructions as possible.

Explanation / Answer

#include <stdio.h>
int main(int argc, char* argv[])
{
    printf("Hello World!");
    return 0;
}

PUBLIC _main
PUBLIC ??_C@_0N@GCDOMLDM@Hello?5World?$CB?$AA@ ; `string' EXTRN   _printf:NEAR
; COMDAT ??_C@_0N@GCDOMLDM@Hello?5World?$CB?$AA@ ; File g:wksrccompoutmain.cpp CONST   SEGMENT
?_C@_0N@GCDOMLDM@Hello?5World?$CB?$AA@ DB 'Hello World!', 00H ; `string' ; Function compile flags: /Ogty CONST   ENDS
; COMDAT _main _TEXT   SEGMENT
argc$ = 8
_argv$ = 12
_main   PROC NEAR ; COMDAT ; Line 5     push    OFFSET FLAT:??_C@_0N@GCDOMLDM@Hello?5World?$CB?$AA@
    call    _printf
    add esp, 4
; Line 6     xor eax, eax
; Line 7     ret 0
_main   ENDP
END