'做这项工作' - 汇编学校项目

时间:2014-03-14 13:35:59

标签: assembly x86 dos

我在组装方面遇到了一些问题。

这是我的学校项目 - 我的老师说只做'做这个工作'(这段代码应该成对地交换字母)并且没有说装配甚至是如何工作的。

我不希望你为我做任何事情,但提示会很完美。

    .MODEL  TINY

Cod SEGMENT

    ORG   100h
    ASSUME CS:Cod, DS:Cod, SS:Cod 

Start:

    jmp Poczatek 

Text   DB  'Some text$'

Beginning:

    mov ax, OFFSET Text
    mov  ds, ax
    mov  bx, 0

Loop:

    cmp ax, "$"
    mov al, [bx]
    je  Show
    inc bh
    mov [bx], ah
    cmp al, '$'
    mov [bx-1], ax
    je  Show
    mov [bx], bl
    dec bl
    jmp Loop

Show:

    mov ah, 09h
    mov dx, OFFSET Text
    int 21h

    mov ax, 4C00h
    int 21h

Cod  ENDS

END  Start

1 个答案:

答案 0 :(得分:-1)

我尝试评论一些说明以显示它们的作用,因此程序员可以看到这部分代码有什么问题:

Start:

jmp Poczatek               ; jump to?

Text   DB  'Some text$'

Beginning:

mov ax, OFFSET Text
mov  ds, ax
             ; An offset address can not be a segment address.
             ; For to address some ASCII-data stored in the codesegment
             ; we have to use the segment address of the codesegment
             ; together in combination with an offset address of the codesegment
             ; Example for to store the address of the codesegment into the data
             ; segment register: "mov ax,cs" + "mov ds,ax"

mov  bx, 0   ; The offset address of the ASCIIs is not zero.
             ; Use "mov bx,OFFSET Text" for to get the offset address.

Loop:

cmp ax, "$"  ; "$"= ASCII "24h" is only one byte, but with AX we compare two bytes = "??24h",
             ; so the result can only be equal if AL=24 and AH=0. But from the last instruction
             ; of the AX-register above we get the low and the high byte of an address.
             ; We do not load the AX-Register with two ASCIIs of the text-buffer.

mov al, [bx] ; We get one byte from the address of DS:BX; first match results AL="S", but only
             ; if we set the DS-register to the address of the codesegment and additional
             ; if we set BX to the offset address of the text-buffer.

je  Show     ; We jump only if the zero-flag is set from the compare-instruction above.

inc bh       ; We increase the high byte of the BX offset register,
             ; so we step 256 bytes forward with it.

mov [bx], ah ; We store the high byte of an address in AH into the codesegmet at DS:BX.
             ; BX is pointed to the offset of "Text" + 256 Bytes.

cmp al, '$'
mov [bx-1], ax ; We store the first ASCCI in AL("S") together with the high byte of
               ; an address in AH to the address in DS:BX-1.

je  Show
mov [bx], bl ; We store the low byte of the offset address in BX to the address of DS:BX
dec bl       ; we decrease the low byte of the offset address in BX

jmp Loop     ; First the BX offset register pointed to the text-buffer,
             ; if we set DS to the address of the codesegment and additional
             ; if we set BX to the offset address of the text-buffer.
             ; But after we increase only the high byte of the offset register
             ; and additional we decrease only the low byte of the offset register,
             ; we loose the pointer to the text-buffer.