[NASM] String Reverse =P

Iniciado por Binary_Death, 28 Noviembre 2010, 19:23 PM

0 Miembros y 1 Visitante están viendo este tema.

Binary_Death

Mi segundo programa. Mi segundo día de estudio de asm  :P
Imagino que usé el método más espantoso para hacerlo... pero bueno, no se casi nada y es sólo mi segundo code de práctica... así que xD

Código (asm) [Seleccionar]

;;;;;;;;;;;;;;;;;
;String Reverse ;
;By Binary_Death;
;;;;;;;;;;;;;;;;;
segment Data
msg1 db 'Cadena a invertir: $'
msg2 db 'Cadena invertida: $'
string times 256 db ' '
char db ' $'
clf db 10,13,10,13,'$'
segment Stack stack
resb 256
StackEnd:
segment Code
..start:
mov ax, Stack
mov ss, ax
mov sp, StackEnd
mov ax, Data
mov ds, ax
mov ah,9h
mov dx,msg1
int 21h
xor bx,bx
SaveStr:
xor ah,ah
int 16h
cmp al,0Dh
je ShowStr
mov [string+bx],al
mov [char],al
mov dx,char
mov ah,9
int 21h
cmp bx,00FFh
je ShowStr
inc bx
jmp SaveStr
ShowStr:
mov ah,9h
mov dx,clf
int 21h
mov dx,msg2
int 21h
dec bx
LoopPos:
mov al,[string+bx]
mov [char],al
mov ah,9h
mov dx,char
int 21h
cmp bx,00h
je Exit
dec bx
jmp LoopPos
Exit:
xor ah,ah
int 16h
mov ah,4ch
int 21h


Agradecería que se me comentara cómo mejorar.

Saludos!

EDITO:

Bueno, en el tercer día de aprendizaje aprendí unas cuantas nuevas cosas. Así que mejoré esto...

Código (asm) [Seleccionar]

;;;;;;;;;;;;;;;;;
;;StrRev 16 bit;;
;;    NASM     ;;
;;Binary_Death ;;
;;;;;;;;;;;;;;;;;
segment data
MSG1 DB 'Cadena a Invertir: $'
MSG2 DB 'Cadena Invertida: $'
CRNL DB 10,13,'$'
BKSC DB 8,'$'
CHR1 DB ' $'
segment stck stack
resb 256
SpOffset:

segment code
..start:

;Asumimos segmentos
mov ax,data
mov ds,ax ;DS=Data
mov es,ax ;ES=Data

mov ax,stck
mov ss,ax ;SS=Stck
mov sp,SpOffset ;SP=Offset(SpOffset)

mov ah,9h
mov dx,MSG1
int 21h ;Mostramos MSG1 en pantalla

mov dx,0FFh
xor cx,cx
PushLoop:
xor ah,ah
int 16h ;Pedimos carácter por teclado
cmp al,9h ;Si CHR=BS
jz BackSpace ;jmp BackSpace
cmp al,0Dh ;Si chr=\CR
jz ShowStr ;jmp ShowStr
push ax ;Empujamos AH+AL(=00+Chr) a stack
mov [CHR1],al ;Guardamos carácter en memoria
mov ah,9h
mov dx,CHR1
int 21h ;Mostramos el carácter guardado
inc cx ;Incrementamos CX
dec dx ;Decrementamos DX
jnz PushLoop ;Si CX!=0 jmp PushLoop

ShowStr:
mov ah,9h
mov dx,CRNL ;Salto de línea
int 21h ;Efectuamos

PopLoop:
pop ax ;Recuperamos último CHR
mov [CHR1],al ;Lo guardamos en CHR1
mov ah,9h
mov dx,CHR1
int 21h ;Lo mostramos en pantalla
loop PopLoop ;Si CX!=0 jmp PopLoop

Exit:
xor ah,ah
int 16h ;Hacemos pausa
mov ah,4ch
int 21h ;Devolvemos control al OS

BackSpace:
mov ah,9h
mov dx,BKSC
int 21h
jmp PushLoop