          .ORIG  x3400

;; R0 is address of packet
IPcksm    LDR    R2,R0,#0         ;; R2 <- IP header word 0
          LD     R3,LenMsk
          AND    R2,R2,R3

;; R2 is length of packet in 32-bit words shifted four places
;;       length of packet in 16-bit words shifted three places

;; clear out the present checksum field
          AND    R3,R3,#0
          STR    R3,R0,#5       

          ADD    R4,R0,#0
          AND    R1,R1,#0         ;; R1 <- 0
;;        Do the loop
CkLoop    LDR    R3,R4,#0
          BRn    T1neg
;; Term 1 (from IP packet) is positive
          ADD    R1,R1,#0
          BRn    diffSign         ;; different signs
;; both are positive, no carry needed
          ADD    R1,R1,R3
          BR     addDone
;; Term 1 (from IP packet) is 
T1neg     ADD    R1,R1,#0
          BRzp   diffSign         ;; different signs
;; both are negative, must add in 1
          ADD    R1,R1,R3
          ADD    R1,R1,#1
          BR     addDone
;; numbers have different signs
diffSign  ADD    R1,R1,R3
          BRn   addDone           ;; Result is negative, no carry to add
          ADD    R1,R1,#1
addDone   ADD    R4,R4,#1
          ADD    R2,R2,#-8
          BRp    CkLoop
          NOT    R1,R1
          RET

LenMsk    .FILL  x00F0
          .END

;    0                   1                   2                   3   
;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;   |Version|  IHL  |Type of Service|          Total Length         |
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;   |         Identification        |Flags|      Fragment Offset    |
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;   |  Time to Live |    Protocol   |         Header Checksum       |
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;   |                       Source Address                          |
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;   |                    Destination Address                        |
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;   |                    Options                    |    Padding    |
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
;
;                    Example Internet Datagram Header

