bài tập vi điều khiển được biên soạn theo chương trình chuẩn của Bộ giáo dục và đào tạo năm 2016. Bài tâp vi điều khiển giúp người học tiếp thu nhanh và cọ sát với đề thi cuối kỳ. Bài tập vi điều khiển trường đại học bách khoa đại học đà nẵng được biên soạn theo đề cương ôn thi cuối kỳ
Trang 1Chương 8 Câu 1: Tìm số nguyên tố từ 100 đến 200
Main.s
AREA mycode,CODE,READONLY
EXPORT main
IMPORT soNguyenTo
ALIGN
ENTRY
main proc
LDR r5,=ketqua
mov r4,#100
loop CMP r4,#200
BGT stop
MOV r0,r4
BL soNguyenTo
cmp r0,#1
STREQ r4,[r5],#4
ADD r4,r4,#1
B loop
stop B stop
endp
AREA myData,CODE,READWRITE
ALIGN ketqua DCD 0 end
soNguyento.s
AREA codene,CODE EXPORT soNguyenTo ALIGN
soNguyenTo PROC
;R0 so kiem tra PUSH {LR}
CMP r0,#0x02;xet n<2 BGE Continue
B KetThuc Continue MOVS r1,#0x02 ;r1 = i;
B CheckVongFOr
; if(n%i==0)break;
Loop SDIV r2,r0,r1 MLS r2,r1,r2,r0 CBNZ r2,LenhTiepTuc
B LenhBreak LenhTiepTuc ADDS r1,r1,#1 CheckVongFOr mov r3,r0;,LSR#1; ADD r3,r0,r0,LSR #31; n/2
CMP r1,r3,LSR #1 BLT Loop LenhBreak NOP
;if(i==n/2) ADD r3,r0,r0,LSR #31 CMP r1,r3,ASR #1 BNE KetThuc
; int result = 1;
MOVS r0,#0x01
B Thoat KetThuc NOP MOVS r0,#0x00 Thoat NOP
POP {LR}
BX LR ENDP END
Cau 2: Viet chuong trình truyen 8 đối số
Main.s
AREA mycode,CODE,READONLY
EXPORT main
import extraargument
ALIGN
ENTRY
main proc
mov r0,#5
mov r1,#5
mov r2,#5
mov r3,#5
mov r4,#5
push {r4}
mov r4,#6
push {r4}
mov r4,#7
push {r4}
mov r4,#8
push {r4}
BL extraargument
LDR r5,=ketqua
STR r0,[r5]
stop B stop
endp
AREA myData,CODE,READWRITE
ALIGN
ketqua DCD 10
end
cau8_2.s
AREA codene,CODE
export extraargument
align
extraargument proc
adds r0,r0,r1
adcs r0,r0,r2
adcs r0,r0,r3
pop {r4,r6,r5,r7}
adds r0,r0,r4
adcs r0,r0,r5
adcs r0,r0,r6
adc r0,r0,r7
;mov r1,#0
;loop cmp r1,#4
;BGE exit
;pop {r2}
;adcs r0,r0,r2
;add r1,r1,#1
;b loop
exit
bx lr
endp end
Cau3: dịch chuyển kí tự asci Main.s
AREA mycode,CODE,READONLY EXPORT main
import shiftEncryption ALIGN
ENTRY main proc LDR r0,=ketqua MOV r1,#1
BL shiftEncryption stop B stop
endp AREA myData,CODE,READWRITE ALIGN
ketqua DCB "ABCDEF$$HIHA",0 end
cau8_3.s
AREA codene, code export shiftEncryption align
shiftEncryption proc
;r0 string shift encryption
;r1 offset mov r2,#0; i = 0 -> n loop LDRB r3,[r0,r2];r3 = str[i]
CMP r3,#0 BEQ exit; neu '\0' exit CMP r3,#'A' ; kiem tra thuoc trong khu vuc ki tu
BLT continue CMP r3,#'Z' BLE shiftEN;
CMP r3,#'a' BLT continue CMP r3,#'z' BGT continue shiftEN
add r3,r3,r1 STRB r3,[r0,r2]
continue add r2,r2,#1
B loop exit bx lr endp end
cau 4: MOIVE PRICE main.s
AREA mycode,CODE,READONLY EXPORT main
import moviePrice ALIGN
ENTRY main proc MOV r0,#13
BL moviePrice LDR r1,=ketqua STR r0,[r1]
stop B stop endp AREA myData,CODE,READWRITE ALIGN
ketqua DCD 0 end
cau8_4.s
AREA codene,code export moviePrice align
moviePrice proc
;r0 so tuoi dau vao
;r1 gia ve dau ra CMP r0,#12 MOVLE r0,#6 BLE exit CMP r0,#64 MOVLE r0,#8 BLE exit MOV r0,#7 exit bx lr
endp end
cau 5: tính tổng calculates Sn(a) = a + aa + aaa +
… main.s
AREA mycode,CODE,READONLY EXPORT main
import calculates ALIGN ENTRY main proc MOV r0,#3 mov r1,#5
BL calculates
Trang 2LDR r1,=ketqua
STR r0,[r1]
stop B stop
endp
AREA myData,CODE,READWRITE
ALIGN
ketqua DCD 0
end
cau8_5.s
area codene,code
export calculates
align
calculates proc
push {r3,r4,r5,lr}
mov r3,#0; tong moi so hang
mov r4,#0; tong S
CMP r1,#0;keim tra truong hop n=0
MOVEQ r0,#0
loop
cmp r1,#0; n <= 0; exit
BLE exit
mov r2,r1;r2 = n-i
mov r3,#1; tinh tich P moi so hang
loopa
cmp r2,#0; r2 == 0 break;
BLE continue
mul r3,r3,r0
sub r2,r2,#1
B loopa
continue
add r4,r4,r3
sub r1,r1,#1
B loop
exit
mov r0,r4; chuyen S -> r0
pop {r3,r4,r5,lr}
bx lr
endp
end
cau 6: Viết chương trình tính tổng giai thừa từ o
-> 10
AREA mycode,CODE,READONLY
EXPORT main
ALIGN
ENTRY
main proc
BL sum
LDR r1,=ketqua
STR r0,[r1]
stop B stop
endp
factorial proc
CMP r0,#0
MOVEQ r0,#1
BXEQ lr
mov r3,#1
loopfactorial
cmp r0,#0
BLE exitfactorial
mul r3,r3,r0
sub r0,r0,#1
B loopfactorial
exitfactorial
mov r0,r3; N! > r0
bx lr
endp
sum proc
push {r4,r5,lr}
mov r4,#10
mov r5,#0
loopsum
cmp r4,#0 BLE exitsum mov r0,r4
BL factorial ADD r5,r0 sub r4,r4,#1
B loopsum exitsum
mov r0,r5 pop {r4,r5,lr}
bx lr endp AREA myData,CODE,READWRITE ALIGN
ketqua DCD 0 end
câu 7: đếm số bít 1
AREA mycode,code, readonly export main
align entry main proc mov r0,#10
BL countbit1 LDR r1,=ketqua str r0,[r1]
endp countbit1 proc
;r0 dem so bit 1 mov r1,#32; i 0-> 32 mov r2,#0 ; result loopcount
cmp r1,#0 BLE exitcount LSLS r0,r0, #1 adc r2,r2,#0 sub r1,r1,#1
B loopcount exitcount
mov r0,r2
bx lr endp area mydata,code,readwrite align
ketqua dcd 0 end
cau 8 : đếm số bít khác nhau của 2 số main.s
AREA sum,CODE EXPORT main IMPORT countbitdiffer ALIGN
ENTRY main PROC LDR r0,=so1;
LDR r0,[r0]
LDR r1,=so2;
LDR r1,[r1]
BL countbitdiffer LDR r1,=result;
STR r0,[r1]
stop B stop ENDP AREA myData,DATA so1 DCD 13
so2 DCD 0xF000000F result DCD 2 END
Cau8_8.s
AREA sum,CODE EXPORT countbitdiffer ALIGN
countbitdiffer PROC
;dàu vao r0,r1
;thuc hien xor r0,r1 de tinh ra bit khac nhau
; tra ve so bit khac nhau r0 PUSH {LR}
EOR r2,r0,r1
; so bit khac nhau tuong duong so bit 1 co trong r2
; dem so bit 1 trong r2 MOV R0,#0 MOV R1,#0 MOV r3,#32; bien i =32;
Loop LSLS r2,r2,#1 ; dich trai ADC r0,r0,#0
SUB r3,r3,#1 ; i = i-1
POP {LR}
BX LR ENDP END Câu 10: tìm hiểu về stack AREA sum,CODE EXPORT main IMPORT countbitdiffer ALIGN
ENTRY main PROC mov r0,#2
BL quad
b endl sq1
push {lr}
mul r0,r0
bl sq2 pop {pc}
sq2 push {lr}
mul r0,r0 pop {pc}
quad push {lr}
mul r0,r0
bl sq1 pop {lr}
BX lr endl stop B stop ENDP AREA myData,DATA so1 DCD 13
so2 DCD 0xF000000F result DCD 2 END
Trang 3Cau 11: tính f = ……
Main.s
AREA codene,CODE,READONLY
EXPORT main
IMPORT cal
ALIGN
ENTRY
main PROC
mov r0,#1
mov r1,#1
BL cal
LDR r1,=result
STR r0,[r1]
stop B stop
ENDP
AREA myData,CODE,READWRITE
export const_a
export const_b
export const_c
align
const_a dcd 1
const_b dcd 2
const_c dcd 1
result DCD 223
END
Cau8_11.s
area codede,code
export cal
import const_a
import const_b
import const_c
align
cal proc
; r0 = x
; r1 = y
; r2 = ax2
; r3 = bxy
; r4 = r2 + r3 + c
push {r4,r5,lr}
LDR r2,=const_a
LDR r2,[r2]
mul r2,r2,r0
mul r2,r2,r0
LDR r3,=const_b
LDR r3,[r3]
mul r3,r3,r0
mul r3,r3,r1
LDR r4,=const_c
LDR r4,[r4]
add r4,r4,r3
add r4,r4,r2
mov r0,r4
pop {r4,r5,lr}
bx lr
endp
end
cau 12: tính số Fibonacci
main.s
AREA codene,CODE,READONLY
EXPORT main
IMPORT fibonacci
ALIGN
ENTRY
main PROC
mov r0,#3
BL fibonacci
LDR r1,=result STR r0,[r1]
stop B stop ENDP AREA myData,CODE,READWRITE align
result DCD 223 END
Cau8_12.s area codene,code export fibonacci align
fibonacci proc
;r0 = so fibonacci thu n CMP r0,#0
BXEQ lr CMP r0,#1 BXEQ lr mov r1,#1 ; i : 1-> n mov r2,#0 ; f(n-2) mov r3,#1 ; f(n-1) loop
cmp r1,r0 BGT exit add r3,r3,r2 sub r2,r3,r2 add r1,r1,#1
b loop exit
mov r0,r3
bx lr endp end
cau 13: đối xứng chuỗi main.s
AREA codene,CODE,READONLY EXPORT main
IMPORT palindrome ALIGN
ENTRY main PROC LDR r0,=chuoi
BL palindrome LDR r1,=result STR r0,[r1]
stop B stop ENDP AREA myData,CODE,READWRITE align
chuoi DCB "abcdeedcba",0 result DCD 111
END
Cau8_13.s
area codene,code export palindrome align
palindrome proc mov r4,r0
bl strlen; r0 <- length sub r0,r0,#1 mov r1,#0; r1 <- 0 looppalin
LDRB r2,[r4,r0];
LDRB r3,[r4,r1];
cmp r2,r3 MOVNE r0,#0 BXNE lr cmp r1,r0 BGT successpalin
add r1,r1,#1 sub r0,r0,#1
b looppalin successpalin mov r0,#1
bx lr endp strlen proc
;r0 string
;< length : r0 mov r2,#0 loop
LDRB r1,[r0],#1 cmp r1,#0 BEQ exit add r2,r2,#1
B loop exit
mov r0,r2
bx lr endp end
câu 14: viết đệ qui main.s
AREA codene,CODE,READONLY EXPORT main
IMPORT recursive ALIGN
ENTRY main PROC mov r0,#2 mov r1,#3
BL recursive LDR r1,=result STR r0,[r1]
stop B stop ENDP AREA myData,CODE,READWRITE align
result DCD 111 END
Cau8_14.s
area codene,code export recursive align
recursive proc
;tinh n^k
; n -> r0
; k-> r1 push {lr}
cmp r1,#0 MOVEQ r0,#1 POPEQ {pc}
push {r4}
mov r4,r0 sub r1,r1,#1
bl recursive mul r0,r0,r4 pop {r4}
pop {pc}
endp end
Trang 4Chương 10
Câu 1: xóa một kí tự trong chuối
Main.c
char str1[50] = "abc xyz abc chao den voi
assembly";
char charRemove = 'a';
extern void removeChar(char *s,char c);
int main(void){
//xoa mot ki tu ra khoi chuoi
removeChar(str1,charRemove);
return 0;
}
Removecharr.s
AREA substring,CODE
EXPORT removeChar
ALIGN
removeChar PROC
;r0 str[0]
;r1 char
;bao ton gia tri
PUSH {r4,lr}
mov r2,#0
loopend LDRB r2,[r0],#1; r2 = str1[i]; i++;
CMP r2,#'\0' ;
BEQ endloop
CMP r2,r1
BNE loopend
mov r3,r0
; j = i
swap LDRB r4,[r3],#-1 ;r4,str[j]; j ;
STRB r4,[r3],#2 ;str1[j] =
r4; j+=2;
CMP r4,#'\0'
bne swap
B loopend
endloop POP {r4,lr}
BX LR
ENDP
Cau 2: nối chuối
Main.s
AREA mycode,CODE,READONLY
EXPORT main
IMPORT strcat
ALIGN
ENTRY
main proc
LDR r0,=chuoi
LDR r1,=chuoi1
BL strcat
stop B stop
endp
AREA myData,CODE,READWRITE
ALIGN
diachi DCD 0
chuoi DCB "noi chuoi nay",0
chuoi1 DCB ", voi chuoi nay",0
end
strcat.c
void strcat(char *dst,char *src){
while(*dst) dst++;
while(*dst++ = *src++);
}
Cau 4 tim kí tự trong chuỗi
Main.s
AREA mycode,CODE,READONLY
EXPORT main
IMPORT search
ALIGN ENTRY main proc LDR r0,=chuoi mov r1,#'a'
BL search ldr r1,=ketqua str r0,[r1]
stop B stop endp AREA myData,CODE,READWRITE ALIGN
chuoi DCB "noi chuoi nay",0 ketqua dcd 0
end
searchChar.c
char* search (char *s,char c){
char *p;
for(;*s;s++){
if(*s == c) p=s;
} return p;
}
Cau 5: tính tổng score của students Main.c
int total = 0;
struct Student_T{
char c1;
char c2;
int score;
char c3;
} students[10];
extern int totalScore();
int main(void){
//xoa mot ki tu ra khoi chuoi int i =0;
for(;i<10;i++){
students[i].c1 = 'a';
students[i].c2 = 'b';
students[i].c3 = 'c';
students[i].score = i;
} total = totalScore();
return 0;
}
totalScore.s
area condene,code import students export totalScore align
totalScore proc push {lr}
LDR r0,=students; r0 = students[0]
mov r1,#0; r1 = totalScore mov r2,#0; r2 = i:0-> 10 add r0,r0,#4;r0 => students[i].score loop
cmp r2,#10 BGE exit
;doc gia tri bo nho tai students[i].score LDR r3,[r0],#12
add r1,r1,r3 add r2,r2,#1
B loop exit
mov r0,r1 pop {lr}
bx lr endp end
câu 6: tính max của 4 số main.c
int ketqua = 0;
int so1 = 11;
int so2 = 22;
int so3 = 33;
int so4 = 44;
extern int max4(int a,int b,int c,int d);
int main(void){
//xoa mot ki tu ra khoi chuoi ketqua = max4(so1,so2,so3,so4); return 0;
}
Max4so.s
area codene,code export max4 align max4 proc
;r0-r3 -> so1 - so4
;return max -> r0 push {r4,lr}
mov r4,r0 cmp r4,r1 MOVLT r4,r1 cmp r4,r2 MOVLT r4,r2 cmp r4,r3 MOVLT r4,r3 mov r0,r4 pop {r4,lr}
bx lr endp end
Cau 10_& số đối xứng Main.c
int ketqua = 0;
int so = 123321;
extern int checks(int a);
int main(void){
//xoa mot ki tu ra khoi chuoi ketqua = checks(so);
return 0;
}
Cau10_7.s
area codene,code export checks align checks proc
;r0 -> number
;return r0 = 1(true) | 0(false) push {lr,r4,r5}
mov r4,r0
MOV r2,#10 isPal
CBZ r4,done SDIV r1,r4,r2 ;r1 = n/10 MLS r3,r1,r2,r4 ;r3 = n - r1*10
add r1,r1,r1,LSL #2 ;r1 = 5*sum add r1,r3,r1,LSL #1 ;sum = sum * 10 +r
mov r2,#10
BL isPal done MOV r1,r5 CMP r1,r0 BNE no
B exit
exit pop {lr,r4,r5}
bx lr endp end