; Running example to generate sexy prime numbers (author: fcdt)
; https://codegolf.stackexchange.com/questions/6467/generate-sexy-primes/206760
section .text
global _start
_start:
; Function call
mov ecx, 100 ; print sexy primes up to 100
mov edx, resultList ; pointer to result list
call listSexy
; Output
mov esi, resultList
outputLoop:
cmp dword [esi], 0
je outputFinished
; Prepare output string (like sprintf)
mov edi, outputBuffer
mov byte [edi], '['
inc edi
mov eax, [esi]
call intToStr
mov word [edi], ', '
add edi, 2
mov eax, [esi+4]
add esi, 8
call intToStr
mov word [edi], `]\n`
add edi, 2
; Output to stdout
mov edx, edi
mov ecx, outputBuffer ; pointer to output data
sub edx, ecx ; data length
mov ebx, 1 ; file descriptor (stdout)
mov eax, 4 ; system call number (sys_write)
int 0x80
jmp outputLoop
outputFinished:
mov eax, 1 ; system call number (sys_exit)
int 0x80
; Helper Function to output integers
intToStr:
mov ecx, 10
xor ebx, ebx
loop1:
cdq
div ecx
inc ebx
test eax, eax
push edx
jnz loop1
loop2:
pop eax
or al, '0'
mov [edi], al
inc edi
dec ebx
jnz loop2
ret
; Actual function to get sexy primes
listSexy:
mov ebp, esp ; 89E5 Save initial esp
mov edi, edx ; 89D7 Save second parameter in edx
; Part One: Save all prime numbers from 3 to the limit on the stack
; (This takes 32 Bytes)
xor eax, eax ; 31C0 set eax = 1 (shorter than mov eax, 1)
inc eax ; 40
checkComplete:
add eax, 2 ; 83C002 Go to next possible number (first is 3)
cmp eax, ecx ; 39C8 Has the limit, i.e. the parameter, been skipped?
jae partTwo ; 7316
mov ebx, esp ; 89E3 Check it against all previous prime numbers
checkNext: ; which are stored between ebp and esp
cdq ; 99 Clear edx for the later division
push eax ; 50 Put the number on the stack
cmp ebx, ebp ; 39E5 Has the check already completed?
je checkComplete ; 74F1 The comfirmed prime number remains on stack
mov esi, [ebx] ; 8B33 Load a previous prime
div esi ; F7F6
pop eax ; 58
test edx, edx ; 85D2 Is esi a divider?
je checkComplete ; 74E8
add ebx, 4 ; 83C304 Let ebx point to the next possible divisor
jmp checkNext ; EBEC
; Part Zwo: Check all prime numbers if they are sexy primes
; (This takes 39 Bytes)
partTwo:
mov ebx, ebp ; 89E5 Start with the first prime number, i.e. 3
NextPrime:
sub ebx, 4 ; 83EB04 Let ebx point to the next prime number
cmp ebx, esp ; 39E3 Has the last number been reached? It cant be sexy,
; since it has no successors here
jbe Finished ; 761E
mov edx, ebx ; 89DA Check all successors up to a distance of 6
mov eax, [ebx] ; 8B03 eax is the prime number whose successors are checked
add eax, 6 ; 83C006
GoFurther:
sub edx, 4 ; 83EA04
cmp eax, [edx] ; 8B02
jb NextPrime ; 72EB If the successor is too large, x can no longer be sexy
ja GoFurther ; 77F7 If the successor is too small, take the next one
mov eax, [ebx] ; 8B03 Load eax again (shorter than sub eax, 6)
mov ecx, [edx] ; 8B0A Load the successor which makes eax a sexy prime
mov [edi], eax ; 8907 Store both in their intended place
mov [edi+4], ecx ; 894F04
add edi, 8 ; 83C708
jmp NextPrime ; EBDB
Finished:
mov esp, ebp ; 89EC Restore stack
ret ; C3
; Allocate space for result list and output buffer
section .bss
resultList resd 100
outputBuffer resb 16
About Online Asm Compiler
Try our Online Asm Compiler (Version Nasm v2.15.05) to Edit, Run, and Share your Asm Code directly from your browser. This online development environment provides you the latest version Nasm v2.15.05.
How to use Online Asm Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Asm Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Asm Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Asm Compiler: Save and Share Asm Code (Project)
Save Asm Project Online
You can save your Asm Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Asm Project Online
You can use this feature to share your Asm Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Asm Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Asm Compiler
There are several benefits of using the Online Asm Compiler to run your Asm code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.