CMP指令和负数

发布于 2025-01-21 16:47:40 字数 730 浏览 3 评论 0原文

这是一个用汇编语言对给定数组进行排序的代码,

.model small
.stack 100h

.data           ;roll number 2435
data1 db 66h, 2, 045h, 4, 040h, 3, -025h, 5, -010h, 011h
swap db 0
.code
mov ax, @data
mov ds, ax
start:
mov swap, 0
mov bx, 0
loop1:
mov al, [bx+data1]
mov cl, [bx+data1+1]
cmp al, [bx+data1+1]      ;here is the problem when compare 66h with -025h
jbe noswap

mov dl, [bx+data1+1]
mov [bx+data1+1],al
mov [bx+data1], dl
mov swap, 1
noswap:
add bx,1
cmp bx,9
jne loop1

cmp swap,1
je start
mov ah, 04ch
int 21h

它比较数组的所有元素以按升序排序,但是当它与-025h进行比较 66h 时,这意味着 66h是较小的,并且-025h 更大,不交换, not to swap lable

我已经调试了它,发现在后端-025h被存储为DB。我如何正确地用负数正确对数组进行排序

Here is a code to sort the given array in assembly language

.model small
.stack 100h

.data           ;roll number 2435
data1 db 66h, 2, 045h, 4, 040h, 3, -025h, 5, -010h, 011h
swap db 0
.code
mov ax, @data
mov ds, ax
start:
mov swap, 0
mov bx, 0
loop1:
mov al, [bx+data1]
mov cl, [bx+data1+1]
cmp al, [bx+data1+1]      ;here is the problem when compare 66h with -025h
jbe noswap

mov dl, [bx+data1+1]
mov [bx+data1+1],al
mov [bx+data1], dl
mov swap, 1
noswap:
add bx,1
cmp bx,9
jne loop1

cmp swap,1
je start
mov ah, 04ch
int 21h

it compares all elements of array to sort in ascending order, but when it compares 66h with -025h, it implies that 66h is smaller and -025h is bigger and does not swap, mov to no swap lable

i have debugged it and found that at backend -025h is being stored as DB. How can I properly sort the array with negative number

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2025-01-28 16:47:40

在条件分支中使用签名的条件代码:例如jle而不是未签名的条件代码jbe。参见 https://sandpile.org/x86/cc.htm 。如果您要求处理器执行未签名的条件,则将-025h为0xDB,即219,大于66h/102。

处理器未读取数据声明,因此看不到负符号。 由于它从未看到声明,因此如果您放置db -25hdb 219db 0xdb,则对处理器无关紧要所有都以相同的位模式值填充数据。

例如,在C中,我们为变量提供类型,然后编译器(使用语言规则)生成一致访问变量的计算机代码,即称为其声明的大小,以及是否签名或未签名。

在汇编语言中,我们没有具有表达类型信息的可变声明。 因此,我们必须执行编译器所做的工作:使用机器代码中的适当大小和签名,以便每次访问变量。

Use the signed condition code in the conditional branch: e.g. jle instead of the unsigned condition code jbe. See https://sandpile.org/x86/cc.htm . If you ask the processor to do unsigned condition then it will see -025h as 0xDB, which is 219, so larger than 66h/102.

The processor doesn't read data declarations, so it doesn't see the minus sign.  Since it never sees declarations, it doesn't matter to the processor if you put db -25h or db 219 or db 0xdb — these will all populate the data with the same bit pattern value.

In C, for example, we give types to the variables and then the compiler (using language rules) generates machine code that accesses the variable consistently, i.e. as the size it was declared, and also as to whether signed or unsigned.

In assembly language we don't have variable declarations with expressive type information.  So, we must do the job that compilers do: use the proper size and signed'ness in the machine code for every access to variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文