Here are some examples of assembly code.
Adding two numbers
int a = 1; // Declare the a variable and assign it the value of 1
int b = 2; // Declare the b variable and assign it the value of 1
int c; // Declare the c variable, without initializing it
//compiler directive to enter assembly language code
__asm{
mov eax, a // Load the value of the a variable into the EAX register
mov ebx, b // Load the value of the b variable into the EBX register
add eax, ebx // Add EAX to EBX, and write the result into EAX
mov c, eax // Load the EAX value into the c variable
}
Mystery Code
__asm //compiler directive to enter assembly language code
{
mov eax, 0 //
mov ebx, 0 // ebx will be the holder of our final number
mov ecx, 0 // registers all set to 0
mov edx, 0 //
mov al, cin // move the specification 'b' or 'd' to al
cmp al,'b' // comparison, if b continue, if d jump to decimal
jne decimal // jump
binary: mov eax, intin // move intin (the number being converted) to eax
mov edx, 0 // preparation for division
div ten // division
cmp edx, 0 // comparison, if edx is 0 there was no remainder, if not jump to remain
jne remain // jump
mov intin, eax // store quotient of division in intin
cmp eax, 0 // if eax=0 then you are done
je getout // jump to getout
dececx: mov ecx, twopower // code block to increment twopower, run at end of "binary" if edx=0 and eax!=0, called later if edx=1
add ecx, one // increment ecx
mov twopower,ecx // place in twopower
jmp binary // jump to binary loop top
remain: mov intin, eax // called if there was a remainder, store intin
mov edx, 0 // preparation for multiplication
mov eax, one // initialization
mov ecx, twopower // initialization
cmp ecx, 0 // if ecx=0 then we are at 2^0 (the first digit) so we only need to add one, jump to frstdig
je frstdig // jump
remain2: mul two // continuation of remain if ecx!=0, multiplies eax by two
dec ecx // decrements ecx (the index for what power of two we need to calculate)
cmp ecx, 0 // if ecx=0 we are done
je twodone // jump
jmp remain2 // if ecx!=0 then loop to remain2 again
frstdig: mov eax, one // we were only calculating 2^0 so move one into eax
twodone: add ebx, eax // add the power of two just calculated to our ebx tally register
jmp dececx // jump to dececx
decimal: mov eax, intin // section to convert to binary, move intin into eax initialization
decdiv: mov edx, 0 // preparation for division
cmp eax, 0 // if eax=0 then we are done
je decdone // jump to decdone if so
div two // divide by two
mov a[ecx], edx // move the remainder into array a
add ecx, four // increment ecx (the array index) by four so it points to next array element
jmp decdiv // jump to decdiv to repeat until eax=0
getout: mov intin, ebx // final step, moves ebx (the result) to intin
jmp done // jumps out
decdone: mov edx, negone // final step, places -1 into ebx
mov a[ecx], edx // places this into the array to mark where number completes
done: nop // no operation
} //end of assembly code
For Reference...
def itoa( num, radix=2 ):
if num == 0:
return '0'
isNegative = num > 0
if isNegative:
num = -num
d = []
while num > 0:
num, lastDigit = divmod( num, radix )
d.append( digits[ lastDigit ] )
if isNegative:
d.append( '-' )
d.reverse( )
return ''.join( d )