Skip to content

Instantly share code, notes, and snippets.

@sam-falvo
Created October 8, 2014 23:55
Show Gist options
  • Save sam-falvo/a049f9be36b3e51e8cc2 to your computer and use it in GitHub Desktop.
Save sam-falvo/a049f9be36b3e51e8cc2 to your computer and use it in GitHub Desktop.
Example of decent quality assembly language (ancient code from Dolphin OS project, dating back to 1998 or so. Those were the days.)
[global add64]
[global sub64]
;
; This function adds two 64-bit values, passed by reference on the
; stack. The values are passed by reference because the LCC compiler
; does not support 64 bit integers like GCC does.
;
; result
; summand 2
; summand 1
; return address
add64: push ebp
mov ebp,esp
push eax
push ebx
push ecx
push edx
mov ebx,[ebp+8] ;EBX points to first summand
mov ecx,[ebp+12] ;ECX points to second summand
mov edx,[ebp+16] ;EDX points to destination operand
mov eax,[ebx] ;Add the low 32-bits first
add eax,[ecx]
mov [edx],eax
mov eax,[ebx+4] ;Add the high 32-bits next
adc eax,[ecx+4]
mov [edx+4],eax
pop edx
pop ecx
pop ebx
pop eax
pop ebp
ret
;
; This function subtracts two 64-bit values, passed by reference on the
; stack. The values are passed by reference because the LCC compiler
; does not support 64 bit integers like GCC does.
;
; result
; summand 2
; summand 1
; return address
sub64: push ebp
mov ebp,esp
push eax
push ebx
push ecx
push edx
mov ebx,[ebp+8] ;EBX points to first summand
mov ecx,[ebp+12] ;ECX points to second summand
mov edx,[ebp+16] ;EDX points to destination operand
mov eax,[ebx] ;Add the low 32-bits first
sub eax,[ecx]
mov [edx],eax
mov eax,[ebx+4] ;Add the high 32-bits next
sbb eax,[ecx+4]
mov [edx+4],eax
pop edx
pop ecx
pop ebx
pop eax
pop ebp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment