When returning values on the maths stack you must be very careful. When a function exits there must be a value on the top of the maths stack the pointer to this value needs to be stored in BV_RIP(A6) and D4 has to have a values in it which defines the returned parameter type.
Notice anything missing? Although we are allowed to fetch long integers as parameters, we are not allowed to return them. This is a problem and the usual fix is to convert a long integer to a floating point and return that instead. This will be covered in another thrilling episode !