以汇编语言80x86反转字符串

时间:2014-04-22 02:36:13

标签: string assembly reverse tasm dosbox

在我正在进行的程序中,我必须反转用户输入的字符串。我必须留下用户输入的字样,我提示他们输入它,然后在它下面我要反向打印出来。当我尝试使用Tasm编译器在DOSBox中运行它时,它给出了一个错误,指出"非法内存引用"在第189行,这是包含变量的行,我打算将相反的单词放入。有人可以帮助我找出我做错了什么吗?我将不胜感激!也只有在我的程序中有4个盒子。第一个框我尝试在提示下面打印反向字。其余的框打印用户输入的单词而不是它的反转版本。

   title     fill in title          ;program name
   ;------------------------------------------------------------------
   stacksg segment  para stack 'Stack'  ;define the stack
    db  32 dup(0)   ;32 bytes, might want larger
   stacksg ends
   ;------------------------------------------------------------------
   datasg segment para 'Data'   ;data segment

   paralst Label Byte
   maxlen db 21
   actlen db ?
   dbdata db 21 dup('$')
   outit db 'Enter String: $' ;14 chars minus $
   switch db 21 dup('$')
   datasg ends
   ;------------------------------------------------------------------
   codesg segment para 'Code'   ;code segment
   main proc    far     ;main procedure
assume ss:stacksg, ds:datasg, cs:codesg ;define segment registers
mov    ax, datasg   ;initialize data segment register
mov ds, ax

   ;--------------- --------------------------top left corner
mov ah, 06h
mov al, 00
mov bh, 01000001b ; 4eh
mov ch, 0
mov cl, 0
mov dl, 39 
mov dh, 12 
int 10h
   ;-------------------------------------------top right corner
mov ah, 06h
mov al, 0
mov bh, 11110010b
;mov cx, 0c00h
;mov dx, 184fh
mov ch, 0
mov cl, 39
mov dh, 12
mov dl, 79
int 10h
   ;--------------------------------------------bottom left corner
mov ah, 06h
mov al, 0
mov bh, 11100100b ;yellow
mov ch, 12
mov cl, 0
mov dh, 24
mov dl, 39
int 10h 
    ;------------------------------------------bottom right corner
mov ah, 06h
mov al, 0
mov bh, 01011111b ; magenta 80
mov ch, 12
mov cl, 39
mov dh, 24
mov dl, 79
int 10h


    ;--------------------------------------------------- 1st quad
mov ah, 02h
mov bh, 0
mov dh, 5
mov dl, 5
int 10h
mov ah, 09h
lea dx, outit
int 21h
    ;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
    ; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 7
mov dl, 5
int 10h
call REVERSE
    ;--------------------------------------print output
mov ah, 09h
lea dx, switch
int 21h
    ;----------------------------------------------------2nd quad
mov ah, 02h
mov bh, 0
mov dh, 5
mov dl, 44
int 10h
mov ah, 09h
lea dx, outit
int 21h

    ;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
    ; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 7
mov dl, 44
int 10h
    ;--------------------------------------print output
mov ah, 09h
lea dx, dbdata
int 21h


    ;------------------------------------------------------3rd quad
mov ah, 02h
mov bh, 0
mov dh, 17
mov dl, 5
int 10h
mov ah, 09h
lea dx, outit
int 21h

    ;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
   ; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 19
mov dl, 5
int 10h
    ;--------------------------------------print output
mov ah, 09h
lea dx, dbdata
int 21h

   ;------------------------------------------------------4th quad
mov ah, 02h
mov bh, 0
mov dh, 17
mov dl, 44
int 10h
mov ah, 09h
lea dx, outit
int 21h 


    ;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
    ; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 19
mov dl, 44
int 10h
    ;--------------------------------------print output
mov ah, 09h
lea dx, dbdata
int 21h


mov    ax, 4c00h    ;end processing
int    21h

    main    endp        ;end of procedure
    ;----------------------------------------reverse procedure
    REVERSE PROC NEAR
mov cx, 0
    ;-----figure out actlen here
mov actlen, 0
lea bx, dbdata ;may need to use paralst instead
hi: cmp [bx], '$'
jne sup
inc actlen
inc bx
jmp hi
sup:

    ;------------
mov cx, 0
mov cl, actlen
lea bx, dbdata
add bx, cx
yo: cmp actlen, 0
je hola
mov switch, byte ptr[bx]
dec bx
inc switch 
dec actlen
jmp yo
hola:

 RET
 REVERSE ENDP
    codesg  ends        ;end of code segment
     end    main    ;end of program

1 个答案:

答案 0 :(得分:0)

你不能mov memory, memory。首先将源移动到寄存器中,然后将其放在目的地。
此外,inc switch不会做您认为正在做的事情。您无法在运行时更改switch的地址,因此实际发生的事情是您在 {{1}处递增第一个元素 (好像你写过switch)。如果您需要修改地址;再把它放在一个寄存器中。

例如:

inc [switch]

我没有查看整段代码,所以我不清楚你的反向是否应该复制终结符(lea si, dbdata add si,cx lea di, switch mov al,[bx] jcxz hola ; jump if cx == 0 cld ; clear the direction flag; for stosb yo: mov al,[si] stosb ; [di++] = al dec si loop yo ; if (--cx) jmp yo hola: )。如果它应该,你应该总是至少执行一次循环(然后你不需要'$')。如果不应该,则应将源地址设置为jcxz而不是dbdata + cx - 1