Skip to content

Instantly share code, notes, and snippets.

@topher6345
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topher6345/0c3dac619e575bf46935 to your computer and use it in GitHub Desktop.
Save topher6345/0c3dac619e575bf46935 to your computer and use it in GitHub Desktop.
Integer modulo subroutine implementation in simplified assembly.
; int mod(int a,int b)
; by Topher6345
; Implements a integer modulo function
; for Simple 8-bit Assembler Simulator
; http://schweigi.github.io/assembler-simulator/index.html
JMP start
ARG1: DB 13 ; Variable
ARG2: DB 3 ; Variable
start:
MOV A, [ARG1] ; A = ARG1;
MOV B, [ARG2] ; B = ARG2;
CALL modulo ; MOD(A, B);
MOV D, 232 ; Point to output;
ADD C, 48 ; C = (char)C;
MOV [D], C ; print(C);
DB 0 ; exit();
modulo:
JE was_zero ; if(B == 0) { return 0 };
JC mod_return ; if(B < 0) { return C };
MOV C, A ; C = A;
SUB A, B ; A = A - B;
JAE modulo ; MOD(A, B);
mod_return:
RET ; return C;
was_zero:
MOV C, 0 ; C = 0;
RET ; return C;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment