Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

assembly - x86程序集:即使实现正确,交换功能的输出也不正确(x86 assembly: Output from swap function is incorrect even when the implementation is correct)

I've been working on a swap function in order or eventually implement bubble sort using x86 assembly.

(我一直在研究交换功能,以便最终使用x86程序集实现气泡排序。)

My code also includes a function which refactors given number ( from - to + and vise versa ) for another function later, which I've called before testing the swap function

(我的代码还包含一个函数,该函数可以在以后为另一个函数重构给定的数字(从-到+,反之亦然),在测试交换函数之前我已经调用过)

This is the code so far:

(到目前为止,这是代码:)

org  100h
jmp main

    numToNeg dw -9
    toSwap1 db 'a'
    toSwap2 db 'b'

    param1 equ 8 
    swap1 equ 12
    swap2 equ 14



main: 

    push offset numToNeg
    call refactor

    mov ax, numToNeg
    call print_num     

    PRINTN 

    PRINTN "Before Swap:"

    mov al, toSwap1
    Call print_al_chr ;This function prints the given char inside al
    mov al, toSwap2
    Call print_al_chr 

    push offset toSwap1
    push offset toSwap2

    call swap

    PRINTN

    PRINTN "After Swap:"

    mov al, toSwap1
    Call print_al_chr
    mov al, toSwap2
    Call print_al_chr

PROC refactor 
    push bp
    push bx
    push ax
    mov bp, sp  

    mov bx, [bp + param1] 
    mov ax, [bx]

    not ax
    inc ax 

    mov [bx], ax     

    pop ax
    pop bx
    pop bp
    retn 2

ENDP refactor  

PROC swap 
    push bp
    push ax
    push cx
    push bx
    push di

    mov di, [bp + swap1]
    mov ax, [di]
    mov bx, [bp + swap2]
    mov cx, [bx]
    mov [bx], ax
    mov [di], cx

    pop di
    pop bx
    pop cx
    pop ax
    pop bp
    retn 4
ENDP swap

Now I'm 100% sure that the swap function is correct, so the problem is somewhere in my main function, but I couldn't figure that out by myself.

(现在,我100%确定交换功能正确,因此问题出在我的主函数中,但我自己无法解决。)

The stack looked alright to me when i debugged the code and I'm pretty stumped right now :/

(当我调试代码时,堆栈对我来说还不错,而我现在很沮丧:/)

To be clear, The function "print_al_chr" Is indeed working, and the implementation is inside another file I'm importing each time.

(需要明确的是,函数“ print_al_chr”确实可以正常工作,并且该实现在我每次导入的另一个文件中。)

At the start I thought that the problem might be with the 'equ' that I was using at the start, but I'm pretty sure that the values I have passed are indeed correct, because that in the function I'm pushing 5 registers, which takes 10 bytes of space overall, so the parameters should be in the 12 and 14 position.

(一开始我以为问题可能出在我一开始使用的“ equ”,但是我很确定自己传递的值确实正确,因为在函数中我要压入5个寄存器,总共需要10个字节的空间,因此参数应位于12和14位置。)

Thanks :D

(感谢:D)

EDIT: I changed my function to this:

(编辑:我将函数更改为此:)

PROC swap 
    push bp
    ;push ah
    ;push cl
    push bx
    push di

    mov di, [bp + swap1]
    mov ah, [di]
    mov bx, [bp + swap2]
    mov cl, [bx]
    mov [bx], ah
    mov [di], cl

    pop di
    pop bx
    ;pop cl
    ;pop ah
    pop bp
    retn 4
ENDP swap  

I couldn't push the registers ah and cl, so I just changed the equ values to 8 and 10. But it is still not working, I'm going to think about this meantime.

(我无法将寄存器ah和cl压入,所以我只是将equ值更改为8和10。但是它仍然无法正常工作,我将在此期间进行思考。)

  ask by Kidsm translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are missing a mov bp, sp in this function:

(您缺少此功能中的mov bp, sp :)

PROC swap 
    push bp
    push ax
    push cx
    push bx
    push di

    mov di, [bp + swap1]

You reserved one byte for each variable:

(您为每个变量保留了一个字节:)

    toSwap1 db 'a'
    toSwap2 db 'b'

But then you used 16-bit word loads and stores:

(但是随后您使用了16位字加载和存储:)

    mov di, [bp + swap1]
    mov ax, [di]
    mov bx, [bp + swap2]
    mov cx, [bx]
    mov [bx], ax
    mov [di], cx

As di would point to toSwap1 , the last instruction I quoted here would overwrite both variables.

(正如di指向toSwap1 ,我在这里引用的最后一条指令将覆盖两个变量。)

(This is what would happen if you had set bp correctly.)

((如果您正确设置了bp ,就会发生这种情况。))


You are missing a program termination call at the end of your main function:

(您在main功能的末尾缺少程序终止调用:)

main: 

    push offset numToNeg
    call refactor
[...]
    mov al, toSwap2
    Call print_al_chr

PROC refactor 

If this is indeed for a 86-DOS-compatible system, you should add the following after the last call to print_al_chr :

(如果这确实适用于与86-DOS兼容的系统,则应在最后一次调用print_al_chr之后添加以下print_al_chr :)

    mov ax, 4C00h
    int 21h

Another note: The param1, swap1, and swap2 equs for the stack frame variables could be closer to where these functions that use them are defined.

(另一个注意事项:堆栈帧变量的param1,swap1和swap2等值可能更接近定义使用它们的函数的位置。)

That would make it easier to verify that they are correct.

(这样可以更轻松地验证它们是否正确。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...