# Assembly 組合語言 ## HAMMING CODES - 通訊上面的翰林編碼,通訊課程會學到的編碼理論 Sender 發送 packet 給 recevier,並且利用 check sum bits 來確認發送的 packet 位元數是否有遭到更改。 ![IMG_0651](https://hackmd.io/_uploads/H1WAEjl4a.jpg) ![IMG_0653](https://hackmd.io/_uploads/S10V8sxNa.jpg) 透過加上一個 parity bit 來達成 parity even(偶數個1) ![IMG_0654](https://hackmd.io/_uploads/rJ6lwigE6.jpg) 舉例原本為 7-bit number 的 **101011** 使用 check sum bit 來確認位元是否有被串改,並最後透過插入c0,c1,c2,c3的值更改為 12-bit **101001101011** ![IMG_0657](https://hackmd.io/_uploads/rkA3doxNa.jpg) checking the bits ![IMG_0658](https://hackmd.io/_uploads/Byydk2lE6.jpg) ![IMG_0659](https://hackmd.io/_uploads/SyDXmnlNT.jpg) ![IMG_0661](https://hackmd.io/_uploads/ByupH3eVT.jpg) **舉例來說** `0xAA = 0b 1010 1010` c0 = 0 c1 = 0 c2 = 1 c3 = 0 > c0 checksumbit -> 確保位元數 0, 2, 4, 6, 8, 10 是偶數個1 c1 checksumbit -> 確保位元數 1, 2, 5, 6, 9, 10 是偶數個1 c2 checksumbit -> 確保位元數 3, 4, 5, 6, 11 是偶數個1 c3 checksumbit -> 確保位元數 7, 8, 9, 10, 11 是偶數個1 輸出的12碼應為 `1010 0101 1000` ### Hamming code ```asm= LDR r7, =0x40000000 MOV r6, #4 ADR r1, arraya ; start of constants main MOV r2, #0 ; clear out transmitting reg LDRB r0, [r1] ; ; calculate c0 using bits 76543210 ; * ** ** MOV r4, r0 ; make a copy EOR r4, r4, r0, ROR #1 ; 1 XOR 0 EOR r4, r4, r0, ROR #3 ; 3 XOR 1 XOR 0 EOR r4, r4, r0, ROR #4 ; 4 XOR 3 XOR 1 XOR 0 AND r2, r4, #1 ; ; calculate c1 using bits 76543210 ; ** ** * MOV r4, r0 EOR r4, r4, r0, ROR #2 ; 2 XOR 0 EOR r4, r4, r0, ROR #3 ; 5 XOR 3 XOR 2 XOR 0 EOR r4, r4, r0, ROR #6 AND r4, r4, #1 ORR r2, r2, r4, LSL #1 ; ; calculate c2 using bits 76543210 ; * *** ROR r4, r0 EOR r4, r4, r0, ROR #2 ; 2 XOR 0 EOR r4, r4, r0, ROR #3 ; 3 XOR 2 XOR 0 EOR r4, r4, r0, ROR #5 ; 5 XOR 3 XOR 2 XOR 0 EOR r4, r4, r0, ROR #6 ; 6 XOR 5 XOR 3 XOR 2 XOR 0 AND r4, r4, #1 ORR r2, r2, r4, LSL #1 ; ; calculate c3 using bits 76543210 ; **** ROR r4, r0, #4 ; get bit 4 EOR r4, r4, r0, ROR #5 ; 5 XOR 4 EOR r4, r4, r0, ROR #6 ; 6 XOR 5 XOR 4 EOR r4, r4, r0, ROR #7 ; ; 7 XOR 6 XOR 5 XOR 4 ; ; build the final 12-bit result ; ORR r2, r2, r4, ROR #25 AND r4, r0, #1 ORR r2, r2, r4, LSL #2 BIC r4, r0, #0xF1 ORR r2, r2, r4, LSL #3 BIC r4, r0, #0x0F ORR r2, r2, r4, LSL #4 STR r2, [r7], #4 ADD r5, r5 SUB r6, r6 CMP r6, #0 BNE main done B done ALIGN arraya DCB 0xB5 DCB 0xAA DCB 0x55 DCB 0xAA END ``` #### 11/21 正課複習 - 1. Get received checksum bits c0, c1, c2, c3 from the received 12 bits and respectively store in r6, r7, r8, r9. - (1)Get bit 0 from the received 12 bits - Store 12 bits into r0 -> r0 = 0 or 1 -> c0 = 0 or 1 ```asm= ORR r2, r2, r4, ROR #25 ; rotate left 7 bits AND r4, r0, #1 ; get bit 0 from original ORR r2, r2, r4, LSL #2 ; add bit 0 into final BIC r4, 0, 0xF1 ; get bits 3, 2, 1 ORR. r2, r2, r4, LSL #3 ; add bits 3, 2, 1 to final BIC r4, r0, #0x0F ; get upper nibble ORR r2, r2, r4, LSL #4 ; r2 now contains 12 bits ; with checksums MOV r5, #1 AND r6, r5, r2 ;c0 AND r7, r5, r2, ROR #1 ; c1 AND r8, r5, r2, ROR #3 ; c2 AND r9, r5, r2, ROR #7 ; c3 ``` - (2) Get bit 1 from the received 12 bits - (3) Get bit 3 from the received 12 bits - (4) Get bit 7 from the received 12 bits - 2. Get the received data bits from the received 12 bits(get d7~d0 from the received 12 bits) adn store into r11. ```asm= AND r7, r5, r2, ROR #1 AND r8, r5, r2, ROR #3 AND r9, r5, r2, ROR #7 LDR r11, =0 LDR r10, =0x4 AND r4, r2, r10 ORR r11, r4, ROR #2 LDR r10, =0x70 AND r4, r2, r10 ORR r11, r4, ROR #3 LDR r10, 0xF00 AND r4, r2, r10 ORR r11, r4, ROR #4 ``` - 3. Compute checksums bits c0, c1, c2, c3(computed checksum bitsc0 ) ### 第三次隨堂考 #### Part1(a) - r3 = 0x125F2 (result) ```asm= LDR r0, =100 LDR r1, =400 LDR r2, =0x4000000 LDR r3, =0 LOOPa ADD r3, r0 ADD r0, #1 CMP r0, r1 BLE LOOPa STR r3, [r2, #0x20] ``` #### Part2(b)~(c) Using CMP ```asm= LDR r0, =100 LDR r1, =400 LDR r2, =0x4000000 LDR r3, =0 LOOPb TST r0, #3 ADD r3, r0 ADD r0, #1 CMP r0, r1 BLE LOOPb STR r3, [r2, #0x30] ;------------------------ LDR r0, =100 LDR r1, =400 LDR r2, =0x40000000 LDR r3, =0 LOOPc TST r0, #7 ADDEQ r3, r0 ADD r0, #1 CMP r0, r1 BLE LOOPc STR r3, [r2, #0x40] ``` #### Part2(a)~(b) Without CMP ```asm= LDR r0, =100 LDR r1, =401 LDR r2, =0x40000000 LDR r3, =0 LDR r4, =0xFFFFFFFF LOOPa ADD r3, r0 ADD r0, #1 EOR r0, r4 TST r0, r1 EOR r0, r4 BNE LOOPa STR r3, [r2, #0x20] ;------------------------ LDR r0, =100 LDR r1, =408 LDR r2, =0x40000000 LDR r3, =0 LDR r4, =0xFFFFFFFF LOOPb ADD r3, r0 ADD r0, #4 EOR r0, r4 TST r0, r1 EOR r0, r4 BNE LOOPb STR r3, [r2, #0x30] ;------------------------ LDR r0, =104 LDR r1, =408 LDR r2, 0x40000000 KDR r3, =0 LDR r4, =0xFFFFFFF LOOPc ADD r3, r0 ADD r0, #8 EOR r0, r4 TST r0, r1 EOR r0, r4 BNE LOOPc STR r3, [r2, #0x40] ``` ### Homework (Hamming Code) 1. Get received checksum bits c0, c1, c2, c3 from the received 12 bits and respectively store in r0, r1, r2, r3 - (1) Get bit 0 from the received 12 bits (if store in r4) - Store 12 bits into r0 => r0 = 0/1 => c0 = 0/1 - (2) Get bit 1 from the received 12 bits (if store in r4) and store it in r0 - AND r1, r4, #2 //0b000000000...010 - ROR r1, #1 //0b0000000000...01 - AND r1, r4, #2 //0b000000000...010 - ORR r0, r0, r1 // r0 = 00000000...000C1C0 - (3) Get bit 3 from the received 12 bits (if store in r4) and store it in r2 - Get bit 3 from the received 12 bits (if store in r4) and store it in r2 of r0 - AND r2, r4, #8 //0b000000000...1000 - ROR r2, #3 - AND r2, r4, #8 //0b000000000...1000 - ORR r0, r0, r2, ROR r2, #1 //00000000...000C1C0 - (4) Get bit 7 from the received 12 bits (if store in r4) and store it in r3 - AND r2, r4, #0x80 //0b000000000...10000000 - ROR r2, #7 //0b000000000...00000001 2. Get the received data bits from the received 12 bits (get d7~d0 from the received 12 bits and store in r4) and store into r5 3. Compute checksum bits c0, c1, c2, c3 (computed bits c0, c1, c2, c3) from the received data bits - `0000000.....00000C3000C20C1C0 => 000000000...00000000C3C2C1C0` (r5) - 將 `c3, c2, c1, c0` 位移至右手邊的位置 - c0 -> r8 - c1 -> r9 - c2 -> r10 - c3 -> r11 4. Comparereceived with computed c0, c1, c2, c3 - ; Compare r0 and with r5 to see the number of different bits -> EOR - (0) NO BIT DIFFERENT => CORRECT DATA BITS RECEIVED - (1) ONE BIT DIFFERENT => CHECKSUM BIT ITSELF INCORRECT AND DATA BITS - (2) TWO BIT DIFFERENT => ONE OF THE RECEIVED DATA BITS INCORRECT - Find n and m and store them in r11, r12 - j = (2^n + 2^m) - 1 - if EOR = 1010, n = 3, m = 1 => j = (2^n + 2^m) - 1 = 9 - (3) THREE BIT DIFFERENT - (4) FOUR BIT DIFFERENT #### 11/28 正課複習 - 1. Get the received data bits fro mthe received 12 bits (get d7~d0 from the received 12 bits(r4) and store into r5). - 假設 Original 8-bit value = 0xB5; - Modified 8-bit value = 0xBA2(d0的值為錯的:1->0) ```asm= LDR r4, =0xBA2 LDR r10, =1 AND r0, r10, r4 AND r1, r10, r4, ROR #1 AND r2, r10, r4, ROR #3 AND r3, r10, r4, ROR #7 ``` ```asm= LDR r4, =0xBA2 LDR r10, =1 AND r0, r10, r4 ; c0 AND r1, r10, r4, ROR #1 ; c1 ORR r0, r1, LSL #2 ADD r1, r10, r4, ROR #3 ; c2 ORR r0, r2, LSL #2 AND r3, r10, r4, ROR #7 ; c3 ORR r0, r3, LSL #3 ``` - 2. Get the received data bits from the received 12 bits (get d7~d0 from the received 12 bits(R4)) and store into r5. ```asm= LDR r4, =0xBA2 LDR r5, =0 LDR r11, =0x4 AND r12, r4, r11 ORR r5, r12, ROR #2 LDR r11, =0x70 AND r12, r4, r11 ORR r5, r12, ROR #3 LDR r11, =0xF00 AND r12, r4, r11 ORR r5, r12, ROR #4 ``` - 3. Compute checksum bits c0, c1, c2, c3(computed checksum bits c0, c1, c2, c3) from the received data bits and respectively store in bit 0~3 of r1. (4/4) ```asm= LDR r4, =0xBA2 LDR r1, =1 LDR r0, 0xA LDR r5, =0x9 EOR r2, r0, r5 ; 2^n+2^m SUB r2, #1 EOR r4, r1, LSL r2 LDR r5, =0 LDR r11, =0x4 AND r12, r4, r11 ORR r5, r12, ROR #2 LDR r11, =0x70 AND r12, r4, r11 ORR r5, r12, ROR #3 LDR r11, =0xF00 AND r12, r4, r11 ORR r5, r12, ROR #4 ``` ```asm= ; ; build the final 12-bit result ; ORR r2, r2, r4, ROR #25 AND r5, r4, #4 ORR r5, r5, r6, ROR #3 ``` ### ADDITION/SUBTRACTION ```asm= ADD r1, r2, r3; r1=r2 + r3 ADC r1, r2, r3; r1=r2+r3+C SUB r1, r2, r3; r1=r2 - r3 SUBC r1, r2, r3; r1=r2-r3+C-1 RSB r1, r2, r3; r1=r3 - r2 RSC r1, r2, r3; r1=r3 - r2+C - 1 ``` ### Constants and Literal Pools - load program into memory and 1st instruction address into PC - next instruction address - Branch and link - (LR = PC) - PC: Program Counter (R15) - LR: Link Register - 使用到函式需要使用到 `BL func1` - Branch and exchange - `BX lr` - PC <-> lr - PC 與 lr 的值互換 - literal pool orgin - `LTORG` - 前面的 literal 值從 LTORG 所索引的地方開始 - literal pool 1 has 0x12345678 - Move Negative - `MVN r2, #0` - 將 r2 的所有數值進行反向 - copy 1's complement of 0 to r2 - `0000010000` -> `1111101111` - 1. fetch net - from memory according to PC - 2. update PC - 3. decode - 4. execute ```asm= BL func1 ; make first instruction BL func2 ; call first subroutine stop B stop ; terminate the program func1 LDR r0, =42 ; => MOV r0, r#42 LDR r1, =0x12345678 ; => LDR r1, [PC, #N] ; where N=offset to literal pool 1 LDR r2, =0xFFFFFFFF ; MVN r2, #0 BX lr ; return from subroutine LTORG ; literal pool 1 has 0x12345678 func2 LDR r3, =0x12345678 ; => LDR r3, [PC, #N] ; N=offset back to literal poo 1 ;LDR r4, =0x87654321 ; if this is uncommented, it fails ; Literal pool 2 is out fo reach! BX lr ; return from subroutine BigTable SPACE 4200 ; clear 4200 bytes of memory, ; starting here END ``` ![IMG_0743](https://hackmd.io/_uploads/HJ96Nw3Hp.jpg) ### 第四次作業題目 - ctrl + f10 (可以直接執行完畢) ```asm= LDMxx r10, {r0, r1, r4} STMxx r10, {r0, r1, r4} ``` - `LDRxx r10, {r0, r1, r4}` - Load Multiple words into memory - `STRxx r10, {r0, r1, r4}` - Store Multiple words into memory ![IMG_0828](https://hackmd.io/_uploads/H1LMY60Up.jpg) ### Qustion: 1. Q1: check sum bit 的值要是如何插入的 A1: 被插入的值是已經被規定好的無需進行更動