Search code examples
stringassemblyx86nasmcpuid

Joining strings from registers and printing them (CPUID)


Starting to learn NASM assembly, I was looking at some assembly questions here in Stack Overflow and found this one here: Concatenating strings from registers and printing them

I believe that this question is not duplicated because I am trying to replicate the code in NASM and also things were not very clear in the other question.

I decided to replicate this code in NASM, but I did not quite understand the MASM code in question.
I learned about CPUID and did some testing programs.

In order, I'd like to know how we can concatenate registers and then print them on the screen USING NASM.

I want to print 'ebx' + 'edx' + 'ecx' because this is how the CPUID output is organized by what I see in GDB.

I called CPUID with eax=1


Solution

  • "String" is not a very precise term. The Vendor Identification String of CPUID/EAX=0 contains only 12 ASCII characters, packed into 3 DWORD registers. There is no termination character like in C nor a length information like in PASCAL. But it's always the same registers and it's always 3*4=12 bytes. This is ideal for the write-syscall:

    section .bss
    
        buff resb 12
    
    section .text
    global _start
    
    _start:
    
        mov eax, 0
        cpuid
    
        mov dword [buff+0], ebx     ; Fill the first four bytes
        mov dword [buff+4], edx     ; Fill the second four bytes
        mov dword [buff+8], ecx     ; Fill the third four bytes
    
    
        mov eax, 4                  ; SYSCALL write
        mov ebx, 1                  ; File descriptor = STDOUT
        mov ecx, buff               ; Pointer to ASCII string
        mov edx, 12                 ; Count of bytes to send
        int 0x80                    ; Call Linux kernel
    
        mov eax, 1                  ; SYSCALL exit
        mov ebx, 0                  ; Exit Code
        int 80h                     ; Call Linux kernel