Lập trình vi điều khiển STM32L152 bài tập chương 7 sách Embedded Systems with ARM CortexM Microcontrollers in Assembly Language and C (Third Edition – Dr Yifeng Zhu)

30 130 10
  • Loading ...
1/30 trang

Thông tin tài liệu

Ngày đăng: 17/01/2018, 23:29

Bài tập có lời giải và code đính kèm chương 7 sách vi điều khiển Embedded Systems with ARM CortexM Microcontrollers in Assembly Language and C (Third Edition – Dr Yifeng Zhu ) , nội dung các bài tập bao gồm: Chuyển đổi chữ thường sang chữ hoa, tìm bội chung nhỏ nhất, tìm lũy thừa x mũ y, xác định năm nhuận, xóa nguyên âm, số dư bằng nhau khi chia cùng một số, số nguyên tố, số chính phương, số từ trong chuỗi, ngày trong tuần, phương sai, tổng đường chéo ma trận, nhân ma trận... LẬP TRÌNH VI ĐIỀU KHIỂN STM32L152 (Sách Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Third Edition) Dr Yifeng Zhu ) Đại Học Bách Khoa Đà Nẵng BÀI TẬP CHƯƠNG SÁCH (Có code đính kèm) 71_ChuyenSangChuHoa INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = &matrix[0], r1 = row, r2 = col, r3 = &row or &col, r4 = a[i][j], r5 = i, r6 = j, r7 = &a[i][0] LDR r0, =matrix ; r0 = &matrix[0] LDR r3, =row LDR r1, [r3] ; so luong hang = LDR r3, =col LDR r2, [r3] ; so luong cot = MOVS r5,#0 B check_i ;i=0 loop_i MOVS r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 dau tien cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV r8,r0 ; a[0][0] = ;MOV r8, #2 ;MUL r4, r4, r8 ; a[i][j] = 2*a[i][j]; STR r4,[r7,r6,LSL #2] ; luu vao bo nho a[i][j] ADDS ; j++ r6,r6,#1 check_j CMP r6,r2 ; so sanh j voi so luong cot BLT loop_j ; for(j = 0; j < 3; j++) ADDS r5,r5,#1 ; neu j >= thi i++ check_i CMP r5,r1 BLT ; so sanh i voi so hang loop_i ; for(i = 0; i < 4; i++) stop B stop ; dead loop & program hangs here; ENDP ALIGN AREA myData, DATA, READWRITE ALIGN matrix DCD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ; moi phan tu 32 bit - ma tran 4-3 row DCD col DCD END 72_BoiChungNhoNhat INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = result, r1 = a, r2 = b, r3 = temp, r4 = a*b, r5 = t = r2, r6 = so du r MOV r1, #3 MOV ;a=3 r2, #7 ;b=7 ; Doi vi tri neu r1 < r2 CMP r1, r2 ; so sanh a va b MOVLT r3, r1 ; temp = a MOVLT r1, r2 ;a=b MOVLT r2, r3 ; b = temp MUL r4, r1, r2 loop CBZ ; r4 = a*b MOV r5, r2 r2, lcm ;t=b ; b = vao lcm UDIV r6, r1, r2 ; kq = a/b MLS ; sodu = a - b*kq r6, r2, r6, r1 MOV r2, r6 ; b = sodu MOV r1, r5 B loop ;a=t lcm UDIV r0, r4, r1 LDR r7, =result STR r0, [r7] stop ENDP ALIGN B ; lcm = (a*b)/gcd ; stop ; dead loop & program hangs here AREA myData, DATA, READWRITE ALIGN result DCD END 73_LuyThuaXY INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = x, r1 = y, r2 = i MOV r0, #2 ;x=2 MOV r1, #-2 ;y=3 MOV r2, #0 ;i=0 MOV r3, #1 LDR r4, =raise CMP r1, #0 BGT y_duong BLT y_am y_duong CMP r2, r1 BGE stop MUL r3, r3, r0 ADD r2, #1 STR r3, [r4] B y_duong ; i++ y_am CMP r2, r1 BGE stop SDIV r3, r3, r0 ADD r2, #1 STR r3, [r4] B y_am stop ; i++ B stop ENDP ALIGN AREA myData, DATA, READWRITE ALIGN raise DCD ; dead loop & program hangs here END 74_NamNhuan INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ;r0 = flag, r1 = nam, r2 = 400, r3 = , r4 = 100, r5 = so du MOV r1, #2100 ; num = 2012 MOV r2, #400 ;i=1 MOV r3, #4 ; MOV r4, #100 ; UDIV r5, r1, r2 ; MLS r5, r2, r5, r1 ; so du = num - r5*i CBZ r5, yes UDIV r5, r1, r3 ; MLS ; so du = num - r5*i CMP r5, #0 r5, r3, r5, r1 BNE nhuan no ; neu khong chia het cho thi khong phai nam ; Nam chia het cho UDIV r5, r1, r4 ; MLS ; so du = num - r5*i - khong chia het cho 100 r5, r4, r5, r1 CMP r5, #0 BNE yes thi la nam nhuan ; neu chia het cho va khong chia het cho 100 BEQ no phai nam nhuan ; neu chia het cho va chia het cho 100 thi ko yes B MOV stop no stop r0, #1 ; set co flag len - la nam nhuan MOV B r0, #0 stop ; khong phai la nam nhuan ; dead loop & program hangs here ENDP ALIGN END 75_RemoveVowelLetter INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = giatri, r1 = =str ; signed number LDR r1, =str loop ; r1 = &str LDRB r0, [r1] ; nap byte CBZ r0, endloop ; dung neu str[i = CMP r0, #'a' ; so sanh str[i] voi voi a MOVEQ r0, #0x00 ; xoa neu ky tu la a STRB r0, [r1] ; luu gia tri r0 byte vao bo nho BEQ next ; vao next neu str[i] = a CMP r0, #'e' MOVEQ r0, #0x00 STRB r0, [r1] BEQ next CMP r0, #'i' MOVEQ r0, #0x00 ; vao next neu str[i] = e STRB r0, [r1] BEQ next ; vao next neu str[i] = i CMP r0, #'o' MOVEQ r0, #0x00 STRB r0, [r1] BEQ next CMP r0, #'u' MOVEQ r0, #0x00 ; vao next neu str[i] = o STRB r0, [r1] BEQ next CMP r0, #'A' MOVEQ r0, #0x00 ; vao next neu str[i] = u STRB r0, [r1] BEQ next CMP r0, #'E' MOVEQ r0, #0x00 ; vao next neu str[i] = A STRB r0, [r1] BEQ next CMP r0, #'I' MOVEQ r0, #0x00 ; vao next neu str[i] = E STRB r0, [r1] BEQ next CMP r0, #'O' MOVEQ r0, #0x00 ; vao next neu str[i] = I STRB r0, [r1] BEQ next ; vao next neu str[i] = O LDR r3, =col LDR r2, [r3] ; so luong cot = MOVS r5,#0 ;i=0 B check_i loop_i MOVS r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV r8,r0 ; a[0][0] = ;MOV r8, #2 ;MUL r4, r4, r8 ; a[i][j] = 2*a[i][j]; STR r4,[r7,r6,LSL #2] ; luu vao bo nho a[i][j] ADDS ; j++ r6,r6,#1 check_j CMP r6,r2 ; so sanh j voi so luong cot BLT loop_j ; for(j = 0; j < 3; j++) ADDS r5,r5,#1 ; neu j >= thi i++ check_i CMP r5,r1 BLT ; so sanh i voi so hang loop_i stop ; for(i = 0; i < 4; i++) B stop ; dead loop & program hangs here; ENDP ALIGN AREA myData, DATA, READWRITE ALIGN matrix DCD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ; moi phan tu 32 bit - ma tran 4-3 row DCD col DCD 4 END 79_SoChinhPhuong INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ;r0 = flag, r1 = num, r2 = i , r3 = i*i MOV r1, #20 ; num = 15 MOV r2, #2 ;i=1 loop CMP r2, r1 BHS no ; so sanh i voi num ; neu i >= num thi nhay ngoai MUL r3, r2, r2 ; r3 = i*i CMP r3, r1 ; so sanh i*i voi num MOV r0, #1 ; la so nguyen to neu so du #0 BEQ yes ; i*i = num thi nhay vao yes ADDNE r2, r2, #1 ; i += BNE loop yes MOV r0, #1 ; set co flag len - la so chinh phuong B stop no MOV r0, #0 ; khong phai la so chinh phuong stop B stop ; dead loop & program hangs here ENDP ALIGN END 710_SoTuTrongChuoi INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT linker main ; make main visible to ENTRY main PROC ; r0 = giatri, r1 = =str, r2 = count ; signed number MOV r2, #1 ; count = LDR r1, =str ; r1 = &str loop LDRB r0, [r1] ; nap byte CBZ r0, endloop ; dung neu str[i = CMP r0, #0x20 ; so sanh str[i] voi ma ascii cua space ADDEQ r2, #1 ; count += neu str[i] = space ADD r1, #1 ; &str += B loop endloop stop ENDP ALIGN B stop ; dead loop & program hangs here AREA myData, DATA, READWRITE ALIGN str DCB "Dai hoc Da Nang", ; mang ky tu END 711_NgayTrongTuan INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = &matrix[0], r1 = row, r2 = col, r3 = &row or &col, r4 = a[i][j], r5 = i, r6 = j, r7 = &a[i][0] LDR r0, =matrix ; r0 = &matrix[0] LDR r3, =row LDR r1, [r3] ; so luong hang = LDR r3, =col LDR r2, [r3] ; so luong cot = MOVS r5,#0 ;i=0 B check_i loop_i MOVS r6,#0 ;j=0 B check_j loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV r8,r0 ; a[0][0] = ;MOV r8, #2 ;MUL r4, r4, r8 ; a[i][j] = 2*a[i][j]; STR r4,[r7,r6,LSL #2] ; luu vao bo nho a[i][j] ADDS ; j++ r6,r6,#1 check_j CMP r6,r2 ; so sanh j voi so luong cot BLT loop_j ; for(j = 0; j < 3; j++) ADDS r5,r5,#1 ; neu j >= thi i++ check_i CMP r5,r1 BLT stop ENDP ; so sanh i voi so hang loop_i ; for(i = 0; i < 4; i++) B stop ; dead loop & program hangs here; ALIGN AREA myData, DATA, READWRITE ALIGN matrix DCD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ; moi phan tu 32 bit - ma tran 4-3 row DCD col DCD END 712_Variance_PhuongSai INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA variance, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = i, r1 = sum, r2 = &array[0], r3 = n- kich thuoc mang, r4 = =n, r5 = array[i] ; kq R8 = 8.5 MOV r0, #0 ;i=0 MOV r1, #0 ; sum = MOV r7, #0 LDR r2, =array array ; var*size = ; r2 = &array[0] - r2 chua dia chi tham chieu cua bien LDR r4, =n ; r4 = &n LDR r3, [r4] ; r3 = n = 10 loop_mean CMP r0, r3 ; so sanh i voi kich thuoc mang BEQ mean ; i = 10 thi vao mean LDR r5, [r2] ; nap array[i] vao r5 ADD r1, r5 ; sum += array[i] ADD r0, #1 ; i += ADD r2, #4 ; i += r2 chua dia chi tiep theo cua array B loop_mean mean UDIV r6, r1, r3 MOV ; mean = sum/n - tinh trung binh cong r0, #0 LDR r2, =array array ; r2 = &array[0] - r2 chua dia chi tham chieu cua bien loop_var CMP r0, r3 BEQ var LDR r5, [r2] ; nap array[i] vao r5 SUB r1, r5, r6 ; r1 = array[i] - mean MLA r7, r1, r1, r7 ; var*size = (array[i] - mean)^2 + var*size ADD r0, #1 ; i += ADD r2, #4 ; i += r2 chua dia chi tiep theo cua array B loop_var var UDIV r8, r7, r3 ; var = r7/n LDR r0, =vari STR r8, [r0] stop B stop ; dead loop & program hangs here;de xem bo nho ENDP ALIGN AREA myData, DATA, READWRITE ALIGN array DCD n 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ; moi phan tu 32 bit DCD 10 vari DCD END 713_TongDuongCheo INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT ENTRY main ; make main visible to linker main PROC ; r0 = &matrix[0], r1 = row, r2 = col, r3 = &row or &col, r4 = a[i][j], r5 = i, r6 = j, r7 = &a[i][0] LDR r0, =matrix ; r0 = &matrix[0] LDR r3, =row LDR r1, [r3] ; so luong hang = LDR r3, =col LDR r2, [r3] ; so luong cot = MOVS r5,#0 ;i=0 B check_i loop_i MOVS r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV r8,r0 ; a[0][0] = ;MOV r8, #2 ;MUL r4, r4, r8 ; a[i][j] = 2*a[i][j]; STR r4,[r7,r6,LSL #2] ; luu vao bo nho a[i][j] ADDS ; j++ r6,r6,#1 check_j CMP r6,r2 ; so sanh j voi so luong cot BLT loop_j ; for(j = 0; j < 3; j++) ADDS r5,r5,#1 ; neu j >= thi i++ check_i CMP r5,r1 BLT ; so sanh i voi so hang loop_i stop ; for(i = 0; i < 4; i++) B stop ; dead loop & program hangs here; ENDP ALIGN AREA myData, DATA, READWRITE ALIGN matrix DCD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ; moi phan tu 32 bit - ma tran 4-3 row DCD col DCD 4 END 714_TheDotProductVector INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = &matrix[0], r1 = row, r2 = col, r3 = &row or &col, r4 = a[i][j], r5 = i, r6 = j, r7 = &a[i][0] LDR r0, =matrix ; r0 = &matrix[0] LDR r3, =row LDR r1, [r3] ; so luong hang = LDR r3, =col LDR r2, [r3] ; so luong cot = MOVS r5,#0 ;i=0 B check_i loop_i MOVS r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV r8,r0 ; a[0][0] = ;MOV r8, #2 ;MUL r4, r4, r8 ; a[i][j] = 2*a[i][j]; STR r4,[r7,r6,LSL #2] ; luu vao bo nho a[i][j] ADDS ; j++ r6,r6,#1 check_j CMP r6,r2 ; so sanh j voi so luong cot BLT loop_j ; for(j = 0; j < 3; j++) ADDS r5,r5,#1 ; neu j >= thi i++ check_i CMP r5,r1 BLT ; so sanh i voi so hang loop_i stop ; for(i = 0; i < 4; i++) B stop ; dead loop & program hangs here; ENDP ALIGN AREA myData, DATA, READWRITE ALIGN matrix DCD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ; moi phan tu 32 bit - ma tran 4-3 row DCD col DCD 4 END 715_NhanMaTran INCLUDE stm32l1xx_constants.s ; Load Constant Definitions INCLUDE stm32l1xx_tim_constants.s ; TIM Constants AREA main, CODE, READONLY EXPORT main ; make main visible to linker ENTRY main PROC ; r0 = &matrix[0], r1 = row, r2 = col, r3 = &row or &col, r4 = a[i][j], r5 = i, r6 = j, r7 = &a[i][0] LDR r0, =matrix ; r0 = &matrix[0] LDR r3, =row LDR r1, [r3] ; so luong hang = LDR r3, =col LDR r2, [r3] ; so luong cot = MOVS r5,#0 ;i=0 B check_i loop_i MOVS r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV r8,r0 ; a[0][0] = ;MOV r8, #2 ;MUL r4, r4, r8 ; a[i][j] = 2*a[i][j]; STR r4,[r7,r6,LSL #2] ; luu vao bo nho a[i][j] ADDS ; j++ r6,r6,#1 check_j CMP r6,r2 ; so sanh j voi so luong cot BLT loop_j ; for(j = 0; j < 3; j++) ADDS r5,r5,#1 ; neu j >= thi i++ check_i CMP r5,r1 BLT ; so sanh i voi so hang loop_i stop ; for(i = 0; i < 4; i++) B stop ; dead loop & program hangs here; ENDP ALIGN AREA myData, DATA, READWRITE ALIGN matrix DCD 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ; moi phan tu 32 bit - ma tran 4-3 row DCD col DCD END ... r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 dau tien cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV... r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV... r6,#0 B check_j ;j=0 loop_j MUL r7,r5,r2 ; r7 = i * so luong cot ADD r7,r0,r7,LSL #2 cua hang thu i ; r7 = &matrix[0] + * r7 = &a[i][0] - phan tu dau tien LDR r4,[r7,r6,LSL #2] ; r4 = a[i][j] MOV
- Xem thêm -

Xem thêm: Lập trình vi điều khiển STM32L152 bài tập chương 7 sách Embedded Systems with ARM CortexM Microcontrollers in Assembly Language and C (Third Edition – Dr Yifeng Zhu), Lập trình vi điều khiển STM32L152 bài tập chương 7 sách Embedded Systems with ARM CortexM Microcontrollers in Assembly Language and C (Third Edition – Dr Yifeng Zhu)

Từ khóa liên quan

Gợi ý tài liệu liên quan cho bạn

Nhận lời giải ngay chưa đến 10 phút Đăng bài tập ngay