彙編實現字串輸入並倒序輸出

2021-10-05 01:45:30 字數 2569 閱讀 8802

1、int 21h的1號功能進行字元輸入。

datas segment

maxl db 30 ;定義字串大小做計數

rlen db ? ;儲存實際長度

str1 db 30h dup(?);分配記憶體空間

datas ends

stacks segment

dw 60hdup(?);分配堆疊空間

top label word;棧頂指標

stacks ends

codes segment

assume cs:codes,ds:datas,ss:stacks

start:

mov ax,datas

mov ds,ax

mov ax,stacks

mov ss,ax

lea sp,top

mov cl,maxl

xor ch,ch ;高八位清零

lea si,str1

;初始化

l1: mov ah,1

int 21h ;輸入

cmp al,0dh ;判斷是否為回車輸入結束

je fin

mov [si],al

inc si

loop l1

fin:mov cx,si

sub cx,offset str1;計數迴圈輸出次數

mov rlen,cl;實際輸入個數儲存

l2: dec si

mov dl,[si]

mov ah,2

int 21h

loop l2;逆序輸出

mov ah,4ch

int 21h

codes ends

end start

datas segment

;無資料段定義

datas ends

stacks segment

dw 60hdup(?);分配堆疊空間

top label word;棧頂指標

stacks ends

codes segment

assume cs:codes,ds:datas,ss:stacks

start:

mov ax,datas

mov ds,ax

mov ax,stacks

mov ss,ax

lea sp,top

xor cx,cx ;cx清零

;初始化

l1: mov ah,1

int 21h ;輸入

cmp al,0dh ;判斷是否為回車輸入結束

je l2

push ax

inc cx

jmp l1

l2: pop dx

mov ah,2

int 21h

loop l2 ;輸出

mov ah,4ch

int 21h

codes ends

end start

2、int 21h的0a號功能進行字串輸入。利用堆疊操作。

datas segment

buff db 50

db ?

db 50 dup(?) ;緩衝區

str0 db 0dh,0ah,24h

datas ends

stacks segment

dw 100h dup(?)

top label word

stacks ends

codes segment

assume cs:codes,ds:datas,ss:stacks

start:

mov ax,datas

mov ds,ax

mov ax,stacks

mov ss,ax

lea sp,top

;初始化

lea dx,buff

mov ah,0ah

int 21h;輸入

lea dx,str0

mov ah,9

int 21h;回車換行

mov bx,offset buff

inc bx

mov cl,[bx];字串實際個數存放到cl暫存器中

mov al,cl

xor ch,ch

next: inc bx

push [bx];壓棧儲存

loop next

mov cl,al

disp: pop dx;逐個彈出

mov ah,2

int 21h

loop disp

mov ah,4ch

int 21h

codes ends

end start

關於逆序輸出就可以盡量考慮堆疊。

個人筆記,請多指教。

字串倒序輸出

題目 輸入乙個字串,將該串倒序輸出。例如輸入字串 hello 倒序輸出為 olleh str1 str input 請輸入字串 print 輸入的字元是 s str1 str2 定義乙個空串用來接收倒序後的字串 for i in str1 1 對字串進行倒序輸出 str2 join i 使用str2...

倒序輸出字串

public static void main string args system.out.println result public static string revertstring string str char chars str.tochararray int len chars.le...

OJ字串的倒序輸出

將乙個字串str的內容顛倒過來,並輸出。str的長度不超過100個字元。輸入包括一行。第一行輸入的字串。輸出轉換好的逆序字串。i am a student tneduts a ma i description 將乙個字串str的內容顛倒過來,並輸出。str的長度不超過100個字元。input 輸入包...