简体   繁体   English

程序集 IA32 中的符号扩展

[英]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

  • A is an 8-bit variable A 是一个 8 位变量
  • B is a 16-bit variable B 是一个 16 位变量
  • C and D are both 32-bit variables C 和 D 都是 32 位变量

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:一个正确的例子是:

  • A = 0, B = 1, C = 0, D = 0; A = 0, B = 1, C = 0, D = 0; Expected = 1 -> Result = 1 and It works for this example预期 = 1 -> 结果 = 1 并且适用于此示例

The error appears when:在以下情况下会出现错误:

  • A = 0, B = 0, C = 0, D = 1; A = 0,B = 0,C = 0,D = 1; Expected = -1 -> Result = 256预期 = -1 -> 结果 = 256

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM