組合語言實驗 統計字串中的字元與數字數量

2021-10-01 02:41:12 字數 3402 閱讀 6529

題目: 分類統計字元個數

內容: 程式接受使用者從鍵盤輸入的一行字元(字元個數不超過 80 個字元, 該

字串以回車符結束), 並按字母、 數字及其他字元分類統計個數, 然後將相

應的結果存放於 letter、 digit 和 other 中, 並在顯示器上顯示如下資訊:

the counted result of the program:

letter:× ×

digit:× ×

other:× ×

× × 表示乙個十進位制數

**

assume cs:code,ss:stack,ds:data

data segment

db 0,0

,0note db 'please enter the sentence:'

,0dh,

0ah,

'$' str1 db 'the counted result of the program:'

,0dh,

0ah,

'$' str2 db 'letter:'

,'$'

str3 db 'digit :'

,'$'

str4 db 'other :'

,'$'

data ends

stack segment

db 100

dup(0)

stack ends

code segment

start:

mov ax,data

mov ds,ax

mov ah,

09h mov dx,offset note

int21h ;列印note字串

mov ax,stack

mov ss,ax

mov sp,

100

input:

read:

mov ah,

01h ;輸入字元

int21h ;輸入乙個字元,存入al

cmp al,

0dh ;判斷字元是不是回車

jz outstr ;若是回車,跳轉到next,句子輸入完畢

cmp al,

'0';與0作比較

jl other ;比0小,跳轉到other,屬於其他字元,儲存在ds:[2

]中 cmp al,

'9';比較字元與9的關係

jg nextread ;如果比9大,判定不屬於數字,繼續判斷

jl num ;若比9小,則屬於數字

num:

inc byte ptr ds:[1

] jmp read

other:

inc byte ptr ds:[2

];d累加加一

jmp read ;繼續讀取下乙個字元

nextread:

cmp al,

'a';判斷與a的關係

jl other ;比a小,該字

9但小於a,屬於其他字元

cmp al,

'z';若比a大,就判斷字元與z的關係

jnle next1 ;

>z,就jmp到next1,繼續判斷

inc byte ptr ds:[0

];若比z小,則在a到z當中,屬於大寫字母,ds[0]

++ jmp read ;繼續讀取下乙個字元

next1:

;用來判斷是其他字元還是小寫字母

cmp al,

'a' jl other ;如果比a小,則是其他字元,跳到other

cmp al,

'z';若比a大,繼續判斷與z的關係

inc byte ptr ds:[0

];如果比z小,用來存小寫字母

jmp read

jmp other ;若比z大,則屬於其他字元,進行跳轉

outstr:

mov ah,

09h mov dx,offset str1

int21h ;輸出提示字串

a:mov ah,

09h mov dx,offset str2

int21h ;輸出字串

mov al,ds:[0

] mov ah,

0;將letter的個數送到ax

call print ;呼叫print子程式

b:mov ah,

09h mov dx,offset str3

int21h ;輸出digit字串

mov al,ds:[1

] mov ah,

0;將digit的個數送到ax

call print ;輸出個數

c:mov ah,

09h mov dx,offset str4

int21h ;輸出字串「other:」

mov al,ds:[2

] mov ah,

0;將str4的個數送到ax

call print ;輸出個數

exit:

mov ah,

4ch int

21h ;結束返回

print:

;輸出個數

mov bx,

10;dx=

10,除以10

mov cx,

0;cx記錄位數,最後輸出字元的個數,記錄迴圈次數

mov dx,

0;dx記錄餘數

s0:;將單個數字依次入棧

div bx ;ax=ax/

10商,dx=ax/

10餘數

push dx ;將餘數dx入棧

inc cx ;位數加一

cwd ;將ax擴充套件到dx.ax

cmp ax,

0;將剩下的數ax與0比較

jne s0 ;若ax!=

0,繼續迴圈

s1:;輸出字元,cx為迴圈次數,乙個數有多少位

pop dx ;棧頂的資料出棧,給dx

add dl,

30h ;轉化為字元

mov ah,

02h int

21h ;dl字元輸出

loop s1 ;迴圈cx次

mov ah,

02h mov dl,

0dh int

21h ;列印回車

mov ah,

02h mov dl,

0ah int

21h ;換行

ret

code ends

end start

實驗結果

組合語言 實現在指定字串中搜尋字元 A

我也不知道我以前怎麼會寫這種東西的,留個紀念 用串操作指令設計程式,實現在指定字串中搜尋字元 a 若該字條串中有字元 a 則將第乙個 a 字元在該字串中的位置記錄在bx暫存器中,若不包含,則使bx 0ffffh。在程式開始查詢指定字元前要求在螢幕上輸出提示資訊 the program is runn...

組合語言 實驗10 編寫子程式 1 顯示字串

assume cs code data segment db hello world 0 data ends code segment start mov dh,8 行 mov dl,3 列 mov cl,3 顏色 mov ax,data mov ds,ax 資料暫存器 mov si,0 資料暫存器...

組合語言實現字串的輸入,輸出

1.了解 int 21h 的09h 號中斷呼叫 輸出字串 lea dx,字串的開頭 或 mov dx,offset字串的開頭 mov ah,09h int 21h 2.在定義字串的時候要在末尾加上 作為字串的結束標誌。3.了解 int 21h 的0ah 號中斷呼叫 輸入字串 lea dx,字串的開頭...