【问题标题】:CMP and JGE not working in Turbo C++CMP 和 JGE 在 Turbo C++ 中不起作用
【发布时间】:2013-03-29 15:26:43
【问题描述】:

我正在使用 C++ 和汇编语言 (8086) 编写一个混合程序,以从数组中找到最小的数字。这是我的代码

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
__int16 a[5],x,y,res;
int i,j;
y=999;

cout<<"\n Enter 5 Numbers:";
for(i=0;i<5;i++)
{
    cin>>a[i];
}

_asm{
    mov bx,y
}

//Finding smallest
for(i=0;i<5;i++)
{
    x=a[i];
    _asm{
        mov ax,x
        cmp ax,bx
        jge nxt
        mov bx,ax
        nxt:
    }
}

_asm{
    mov res,bx;
}

cout<<"\n Smallest Element:"<<res;
getch();
}

上面的代码是用 Visual Studio 2010 编写的,看起来运行良好。但是当我为 Turbo c++ 更新相同的代码时(即将“iostream”更改为“iostream.h”,删除“using namespace std;”,将“__int16”更改为“int”等),它不起作用。执行后产生的答案是错误的。

这是我的 TC++ 程序

#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],x,y,res;
int i,j;
y=999;

cout<<"\n Enter 5 Numbers:";
for(i=0;i<5;i++)
{
    cin>>a[i];
}

_asm{
    mov bx,y
}

//Finding smallest
for(i=0;i<5;i++)
{
    x=a[i];
    _asm{
        mov ax,x
        cmp ax,bx
        jge nxt
        mov bx,ax
    }
    nxt:
}

_asm{
    mov res,bx;
}

cout<<"\n Smallest Element:"<<res;
getch();
}

为什么 TC++ 和 Visual Studio 10 没有给出相同的答案?

【问题讨论】:

  • 你能显示不工作的代码吗?您似乎在说这不是给出错误答案的代码。
  • 查看反汇编程序中的两个可执行文件,找出生成代码中的哪些差异可能导致错误结果。或者更好的是,在调试器中单步调试代码。
  • 如果您在启用调试信息的情况下编译程序并将其加载到Turbo Debugger (td.exe),您将能够看到main 的反汇编以及for(i=0;i&lt;5;i++) 如何破坏您的内联汇编代码。

标签: c++ visual-studio-2010 assembly turbo-c++


【解决方案1】:

您不能期望寄存器在程序集 sn-ps 之间保留它们的值。您有三个程序集 sn-ps,它们之间有 C 块,它们依赖于 bx 保持不变。编译器没有做出这样的承诺。

要么使用内存来存储运行最小值,要么使用单个程序集 sn-p 重新制定。对于后一种方法,您必须在汇编中重写 for 循环和数组访问;这是非常可行的。像这样:

_asm{
mov dx, y ; we'll use dx instead of bx for the running minimum - long story
mov bx, a   ; that's the array pointer
mov si, 0 ; that's our i
loop:
    mov ax, [bx+si*2] ; read a[i] into ax; *2 because int is two bytes
    cmp ax,dx
    jge nxt
    mov dx, ax
    nxt:
    ;Now the for loop stuff
    inc si ; i++
    cmp si, 5 ; compare i to 5
    jl loop   ; if less, continue looping
; End of loop
mov res,dx;
}

我使用 bx 和 si 进行基址+索引内存访问,因为在早期的 x86 CPU 上,您只能使用有限的寄存器子集(bx 或 bp 用于基址,si 或 di 用于索引)进行这种内存访问.如今,您可以使用任何寄存器组合;但我不确定古董 Turbo C 是否会接受。

【讨论】:

  • 谢谢。寄存器没有保留这些值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
相关资源
最近更新 更多