Last active
August 29, 2015 14:02
-
-
Save topher6345/0c3dac619e575bf46935 to your computer and use it in GitHub Desktop.
Integer modulo subroutine implementation in simplified assembly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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