// // Filename: divider.nac // Purpose : N-address code (NAC) implementation for an iterative integer // division algorithm based on the first algorithm in Niclaus Wirth's // "A note on division". // Author : Nikolaos Kavvadias (C) 2011 // Date : 09-Jun-2011 // Revision: 0.3.0 (09/06/11) // Initial version. procedure divider (in u32 x, in u32 y, out u32 q, out u32 r) { localvar u32 r0, q0, y0, y1; S_1: r0 <= mov x; q0 <= ldc 0; y0 <= mov y; y1 <= mov y; S_2 <= jmpun; S_2: y1 <= shl y1, 1; S_2, S_3 <= jmple y1, x; S_3: y1 <= shr y1, 1; q0 <= shl q0, 1; S_4, S_5 <= jmpge r0, y1; S_4: r0 <= sub r0, y1; q0 <= add q0, 1; S_5 <= jmpun; S_5: S_3, S_6 <= jmpne y1, y0; S_6: q <= mov q0; r <= mov r0; }