;; SAMP2.ASM , @CopyLeft by tsaiwn@csie.nctu.edu.w
;;=== for SAMP2.MC ===;;
;samp2.mc ;; by tsaiwn@csie.nctu.edu.tw
;this machine program read N
; and calculate 1+2+...+N
 LDI   1,01  ;   0000: 2101  ; R1=1
 PutS  MSGN  ;   0002: d624  ; print string  "N="
 GETi        ;   0004: d200  ; read integer into R0
 LDI   9,01  ;   0006: 2901  ; R9 = 1
 LDI   5,00  ;   0008: 2500  ; R5 = 0, used to store sum
 ADD   5,5,9 ;   000A: 5559  ; R5 = R5 +R9  :again
 JUMP  9,12  ;   000C: B912  ; if done goto :ok
 ADD   9,9,1 ;   000E: 5991  ; R9 = R9 + 1
 JUMP  0,0A  ;   0010: B00a  ; goto :again
 PutS  MSGSUM  ; 0012: d6 28  ; print "Sum from 1 to "  :ok
 PUTi          ; 0014: d300  ; out  int from R0
 PutS  MSGN2   ; 0016: d626  ; print "="
 MOVE  5,0     ; 0018: 4050  ; move r5 to r0 , the sum
 PUTi          ; 001A: d300  ; out  int from R0
 PutS  CRLF    ; 001C: d620  ; out cr/lf
 Halt          ; 001E:    c000  ; halt
CRLF:  
   DB 0D,0A ; 0020: 0d0a  ; CR/LF  (:20h)
   DB 24,24 ; "$$"     ; 0022: 2424  ; "$$"
MSGN:
    DB  20,4e ; " N"   ; 0024: 204E  ; " N"   (:24h)
MSGN2:
    DB  3d,24 ; '=','$' ; 0026: 3d24  ; "$$"
MSGSUM:    ; DB "Sum from 1 to $$"
     DB 53,75  ; "Su"   ; 0028: 5375  ; Su     (:28h)
     DB 6d,20  ; "m "   ; 002A: 6D20  ; 'm', ' '
     DB 66,72  ;"from 1 to $";  ;; if the Assembler smart enough:-)
           ;     002C: 6672  ; "fr"
     DB 6f, 6d ,20, 31, 20, 74 , 6f, 20 , 24, 24
 HALT      ;     0038: C000 
    END    ; end of the program
