CMP和JGE不能在Turbo c++中工作
CMP and JGE not working in Turbo C++
我正在用c++和汇编语言(8086)编写一个Mix程序,从数组中找到最小的数字。这是我的代码
#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",删除"使用命名空间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给出的答案不一样?
您不能期望寄存器在程序集片段之间保留它们的值。你有三个汇编片段,它们之间有C块,它们依赖于bx
保持不变。编译器不做这样的承诺。
要么使用内存存储运行的最小值,要么使用单个程序集代码段重新定义。对于后一种方法,您必须在汇编中重写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是否会接受。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 链表c++插入,所有情况都已检查,但没有任何工作
- 为什么stream::忽略未按预期工作