www.xbdev.net
xbdev - software development
Friday March 29, 2024
home | contact | Support | Assembly Language What every pc speaks..1010...

     
 

Assembly Language

What every pc speaks..1010...

 

Inline Assembly Juicy Code Snippets

 

With a few lines of C...and a few lines of asm... you can do some great things...

 

 

Get CPU Speed

 

// Simple set of functions to get your CPU speed, seems

// to work okay on intel processors.

 

#include <windows.h> // used for Sleep(..)

#include <stdio.h>   // used for printf(..)

 

unsigned __int64 CycleCount()

{

      _asm _emit 0x0F

      _asm _emit 0x31

}

 

unsigned int CpuSpeed()

{

      unsigned __int64 start, stop;

      unsigned int total;

      start = CycleCount();

      Sleep(1000);

      stop = CycleCount();

 

      stop = stop - start;

      total = (unsigned)(stop/1000000);

      return total;

}

 

void main()

{

      unsigned int speed = CpuSpeed();

 

      printf("cpu speed: %u", speed);

      printf("cpu speed: %u", speed);

      printf("cpu speed: %u", speed);

}

 

 

Many compilers do not support the  RDTSC and CPUID assembly commands. For those that don't, we do the actual opcode values for the RDTSC and CPUID...shown below:

 

RDTSC  __asm _emit 0x0F __asm _emit 0x31
CPUID  __asm _emit 0x0F __asm _emit 0xA2

 

 

 

Simple..Simple String Encription/Decription

 

 

#include <stdio.h>  // printf

#include <string.h> // strlen

 

 

// String Encription/Decription With Assembly

void encriptstring(char* str, int key)

{

      int len = strlen(str);

 

      for(int i=0; i<len; i++)

      {

            int cur=str[i];

 

            __asm

            {

                  mov   eax, cur

                  add eax, key

                  mov cur, eax

            }

 

            str[i] = cur;

      }

}

 

void decriptstring(char* str, int key)

{

      int len = strlen(str);

 

      for(int i=0; i<len; i++)

      {

            int cur=str[i];

 

            __asm

            {

                  mov   eax, cur

                  sub eax, key

                  mov cur, eax

            }

 

            str[i] = cur;

      }

}

 

 

void main()

{

      char str[] = "secret string";

 

      printf("Original String: %s \n", str);

 

      encriptstring(str, 5);

      printf("Encripted String: %s \n", str);

 

      decriptstring(str, 5);

      printf("Decripted String: %s \n", str);

}

 

 

View Output:

Original String: secret string
Encripted String: xjhwjy%xywnsl
Decripted String: secret string

 

Setting Memory To A Fixed Value

 

inline void MemSetWORD( void *pDest, USHORT data, int iCount)

{

      __asm

      {

            mov edi, pDest   ; edi points to our destination in memory

            mov ecx, iCount  ; number of 16 bit words to write

            mov ax,  data    ; our 16bit data value

            rep stosw        ; mov data

      }

}

 

 

inline void MemSetDWORD( void *pDest, DWORD data, int iCount)

{

      __asm

      {

            mov edi, pDest   ; edi points to our destination in memory

            mov ecx, iCount  ; number of 16 bit words to write

            mov eax, data    ; our 32-bit data value

            rep stosd        ; mov data

      }

}

 

 

 

 

 

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.