【问题标题】:Reversing a string in assembly language 80x86用汇编语言 80x86 反转字符串
【发布时间】:2014-06-06 05:25:58
【问题描述】:

在我目前正在执行的程序中,我必须反转用户输入的字符串。我必须将用户输入的单词留在我提示他们输入的位置,然后在它的正下方我想反向打印该单词。当我尝试使用 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

【问题讨论】:

    标签: string assembly reverse tasm dosbox


    【解决方案1】:

    你不能这样做mov memory, memory。首先将源移动到寄存器中,然后将其放在目标中。
    另外,inc switch 并没有像您认为的那样做。您不能在运行时更改switch 的地址,所以实际发生的情况是您正在增加第一个元素at 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 而不是dbdata + cx

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多