将两个字节 char byte1 和 char byte2 连接成一个 short

concatenate two bytes char byte1 and char byte2 into a single short

我想在汇编中将两个字节 char byte1char byte2 连接成一个 short

我该怎么做?使用班次?

我正在使用 IA32

main.c

#include <stdio.h> 
#include "concatBytes.h" 
char byte1 = '11101010'; 
char byte2 = '10100001'; 
int main()
{ 
    short result = 0; 
    result = concatBytes(); 
    printf("Result = %hd",result); 
    return 0;
}

concatBytes.h

short concatBytes(void);

concatBytes.c

extern char byte1; 
extern char byte2;
short concatBytes(void)
{
    return(((short)byte1)^((short)byte2));
}

很明显:

gcc main.c concatBytes.c -o main
main.c:3:14: warning: character constant too long for its type
 char byte1 = '11101010'; 
              ^
main.c:3:14: warning: overflow in implicit constant conversion [-Woverflow]
main.c:4:14: warning: character constant too long for its type
 char byte2 = '10100001'; 
              ^
main.c:4:14: warning: overflow in implicit constant conversion [-Woverflow]

这是错误的语法。所以这导致了这个问题你是这个意思吗:

#include <stdio.h> 
#include "concatBytes.h" 
char byte1[] = "11101010"; 
char byte2[] = "10100001"; 
int main()
{ 
    short result = 0; 
    result = concatBytes(); 
    printf("Result = %hd",result); 
    return 0;
}

或者这个:

#include <stdio.h> 
#include "concatBytes.h" 
char byte1 = 0xEA; 
char byte2 = 0xA1; 
int main()
{ 
    short result = 0; 
    result = concatBytes(); 
    printf("Result = %hd",result); 
    return 0;
}

假设后者:

0000000000400559 <concatBytes>:
  400559:   55                      push   %rbp
  40055a:   48 89 e5                mov    %rsp,%rbp
  40055d:   0f b6 15 d4 0a 20 00    movzbl 0x200ad4(%rip),%edx        # 601038 <byte1>
  400564:   0f b6 05 ce 0a 20 00    movzbl 0x200ace(%rip),%eax        # 601039 <byte2>
  40056b:   31 d0                   xor    %edx,%eax
  40056d:   66 98                   cbtw   
  40056f:   5d                      pop    %rbp
  400570:   c3                      retq   

这让您大致了解调用约定,然后您只需用“连接”替换该代码的中间部分

如果它是一个字符串,那么你首先需要将每个转换为一个字节,然后连接。你可以很容易地找出一个......

我刚刚解决了这个问题,以防有人遇到同样的问题:

concatBytes.s :

 .section .data

.global byte1
.global byte2

.section .text
.global concatBytes


concatBytes:

#prologue 
    pushl %ebp 
    movl %esp, %ebp
    
    pushl %ebx
    
#body of the function

    movl [=10=], %eax
    movb byte1, %al
    movb byte2, %ah
    

    
#epilogue

    popl %ebx
    
    movl %ebp, %esp
    popl %ebp
    ret