[英]Sign extension in assembly IA32
I'm new to assembly and I'm using IA32 architecture.我是组装新手,我正在使用 IA32 架构。 I'm trying code a .s
function that produces the following operation: C + A - D + B我正在尝试编写一个产生以下操作的.s
函数:C + A - D + B
The function should return a 64-bit value that must be printed in C and I can't figure this out.该函数应该返回一个必须用 C 打印的 64 位值,我无法弄清楚。
I am trying this and it works for the tests of positive numbers, but when I'm working with negative numbers it doesn't work and I can't figure out the reason.我正在尝试这个并且它适用于正数的测试,但是当我使用负数时它不起作用并且我无法找出原因。
My function sum_and_subtract.s
:我的函数sum_and_subtract.s
:
.section .data
.global A
.global B
.global C
.global D
.section .text
.global sum_and_subtract
# short sum_and_subtract(void)
sum_and_subtract:
#prologue
pushl %ebp
movl %esp, %ebp
pushl %ebx
#body of the function
movl $0, %eax # clear eax
movl C, %eax
movl $0, %ecx
movb A, %cl
addl %ecx, %eax
movl $0, %edx
movl D, %edx
subl %edx, %eax
movl $0, %ebx
movw B, %bx
addl %ebx, %eax
movl $0, %edx
adcl $0, %edx
cdq
#epilogue
fim:
popl %ebx
movl %ebp, %esp
popl %ebp
ret
An correct example is:一个正确的例子是:
The error appears when:在以下情况下会出现错误:
After seeing your comments I forgot to write my main code where I print my long long result.看到你的评论后,我忘了写我的主要代码,在那里我打印我的长长的结果。
main.c :主文件:
#include <stdio.h>
#include "sum_and_subtract.h"
char A = 0;
short B = 0;
long C = 0;
long D = 1;
int main(void) {
printf("A = %d\n", A);
printf("B = %hd\n", B);
printf("C = %ld\n", C);
printf("D = %ld\n", D);
long long result = sum_and_subtract();
printf("Result = %lld\n", result);
return 0;
}
Here it is.这里是。
I have this other file sum_and_subtract.h
我有另一个文件sum_and_subtract.h
long long sum_and_subtract(void);
I would follow the C compiler:我会遵循 C 编译器:
doit:
movsbl A(%rip), %eax
movswl B(%rip), %edx
addl C(%rip), %eax
subl D(%rip), %eax
addl %edx, %eax
cltq
ret
movsx eax, BYTE PTR A[rip]
movsx edx, WORD PTR B[rip]
add eax, DWORD PTR C[rip]
sub eax, DWORD PTR D[rip]
add eax, edx
cdqe
ret
It "prints" -1
它“打印” -1
It is rather a comment so do not UV.这是一个评论所以不要紫外线。 Feel free to DV随意DV
The complete code with print: https://godbolt.org/z/3a9YMo带打印的完整代码: https : //godbolt.org/z/3a9YMo
And with the printing code: https://godbolt.org/z/KT8YWT并带有打印代码: https : //godbolt.org/z/KT8YWT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.