Write Pip assembler code equivalent to the Python below. The easiest way and mos
ID: 3853257 • Letter: W
Question
Write Pip assembler code equivalent to the Python below. The easiest way and most encouraged way is using symbolic code labels as needed (followed by a colon) and symbolic data names, as understood by pipGUI.py. You may do it with numeric data addresses and/or numeric jump addresses as in the book or the book's applet, but in that case, do make a comment as to what memory locations represent the x, y, and z of the Python version! if x < 0: y = y + 2 else: y = x z = y + z You are encouraged to write to a file pipa.asm, load it into pipGUI.py, give values manually to x, y, and z, run to test the results. Press INIT and go back to set new x, y, z values and test some more...
Explanation / Answer
This can be done writing C extension wrapper for the code fragment implemented in assembly and then linking to the obj file.
pipa.asm
Symbols Used
X for a symbolic or numeric data address.
#N for a literal number N as data
Acc refers to the accumulator
L refers to a symbolic code label or numeric code address
Instructions Pseudo Python syntax for what happens
Data Flow
LOD X (or #N) Acc = X (or N)
STO X X = Acc (copy Acc to location X)
Control
JMP L IP = L (go to instruction L)
JMZ L if Acc==0: IP = L else: IP = IP+2 (normal)
NOP No operation; just go to next instruction
HLT Halt execution
Arithmetic-Logic
ADD X (or #N) Acc = Acc + X (or N)
SUB X (or #N) Acc = Acc - X (or N)
MUL X (or #N) Acc = Acc * X (or N)
DIV X (or #N) Acc = Acc / X (or N)
AND X (or #N) if Acc != 0 and X != 0: Acc=1 else: Acc=0
NOT if Acc == 0: Acc = 1 else: Acc = 0
CPZ X if X == 0: Acc = 1 else: Acc = 0
CPL X if X < 0: Acc = 1 else: Acc = 0
pip.c
#include <Python.h>
void myfunc(void);
static PyObject*
py_myfunc(PyObject* self, PyObject* args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
myfunc();
Py_RETURN_NONE;
}
static PyMethodDef MyMethods[] =
{
{"myfunc", py_myfunc, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initmyext(void)
{
(void) Py_InitModule("myext", MyMethods);
}
pipGUI.py
from distutils.core import setup, Extension
setup(name='myext', ext_modules=[
Extension('myext', ['myext.c'], extra_objects=['myfunc.obj'])])
X = input('Choose a number')
print ('Number%s ' % (X))
Y = input('Choose a number')
print ('Number%s ' % (Y))
Z = input('Choose a number')
print ('Number%s ' % (Z))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.