Pointers in Assembly Language

The example shows how to work with references to values and memory addresses. Here’s the equivalent code in Python.

In Python, we don’t have pointers explicitly, but we can demonstrate similar behavior using mutable types like lists.

def zeroval(ival):
    ival[0] = 0

def zeroptr(iptr):
    iptr[0] = 0

def main():
    i = [1]
    print("initial:", i[0])
    
    zeroval(i)
    print("zeroval:", i[0])

    zeroptr(i)
    print("zeroptr:", i[0])

    print("reference:", i)
    
if __name__ == "__main__":
    main()

Explanation

We’ll show how reference behavior works primarily using functions zeroval and zeroptr.

  • zeroval has an ival parameter, a list in this case. Hence, changes made to ival will be seen by the caller because lists are mutable and passed by reference.
  • zeroptr also takes a list iptr. There’s no distinction between value and reference parameters for mutable types in Python. Here, the list’s first element is directly modified via the parameter’s reference.

Both zeroval and zeroptr functions modify the same list.

Running the code

To run the program, save it to a file (example.py), and then run it using Python.

$ python example.py
initial: 1
zeroval: 0
zeroptr: 0
reference: [0]

This demonstrates that changes made within the functions affect the original list in the caller.