简体   繁体   中英

How to change this linux Assembly code into compatible UNIX assembly code?

I wish to change this following assembly code into UNIX compatible code without using the linux kernel (or system?) call. (int $0x80)

This code is for Intel 32bit Pentium platform, written in AT&T syntax

#cpuid.s Sample program to extract the processor Vendor ID
.section .data
output:
.ascii “The processor Vendor ID is ‘xxxxxxxxxxxx’\n”
.section .text
.globl _start
_start:
movl $0, %eax
cpuid
movl $output, %edi
movl %ebx, 28(%edi)
movl %edx, 32(%edi)
movl %ecx, 36(%edi)
movl $4, %eax
movl $1, %ebx
movl $output, %ecx
movl $42, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80

thanks,

Suggestion: Write a C program, with inline assembly. You could also call C library functions (like printf ) from your assembly, in order to remove the OS dependency.

Here's an example with inline assembly: http://softpixel.com/~cwright/programming/simd/cpuid.php

At the assembly level, there is no such thing as "UNIX compatible code". Every Unix has its own interface for making system calls.

I can tell you what this code is doing, though. It is calling the CPUID instruction, then putting the result in the "xxx" part of the output string, then calling:

write(1, output, 42);
exit(0);

If you are using GCC, the closest "portable" equivalent looks something like this:

#include <string.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
  int cpuid[3];
  char result[12];
  asm("cpuid"
      : "=b" (cpuid[0]), "=d" (cpuid[1]), "=c" (cpuid[2])
      : "a" (0L));

  memcpy(&result[0], &cpuid[0], 4);
  memcpy(&result[4], &cpuid[1], 4);
  memcpy(&result[8], &cpuid[2], 4);

  printf("The processor Vendor ID is '%s'\n", &result[0]);
  return 0;
}

For other C compilers, you will have to consult the manual to learn their inline asm syntax.

If you really want to call this directly in assembly, you will have to find out how your particular Unix expects system calls to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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