; Use
STRING_TERMINATOR equ 0
SECTION .bss
buffer: resb 10
SECTION .data
thestring: db "1234",0
errormsg: db "ERROR! Invalid input.",0
SECTION .text
; Input:
; ESI = pointer to the string to convert
; ECX = number of digits in the string (must be > 0)
; Output:
; EAX = integer value
string_to_int:
push ebx
push edx
xor edx,edx ; clear edx
xor ebx,ebx ; clear ebx
cmp byte[esi], '-' ; Do we have a minus sign?
jne .next_digit ; If not, just start our loop
mov dl, 1 ; Otherwise, put a 1 in DL and move on for now
dec ecx ; dec ecx because we are done checking the first char
.next_digit:
cmp ecx, 0 ; Have we reached the end of our iteration? Don't move off the rez!
je .done ; if ecx=0 then we're done
movzx eax,byte[esi]
inc esi
cmp al,39h ; Test current char against 39h (highest ASCII digit)
sub al,'0' ; convert from ASCII to number
imul ebx,10
add ebx,eax ; ebx = ebx*10 + eax
dec ecx ; Decrement ecx to keep track that we are done with the current char
loop .next_digit ; while (--ecx)
.done:
mov eax,ebx ; put our numeric value in the accumulator
cmp dl, 1 ; check whether we said this was a negative quantity or not
jne .really_done ; jump to the .really-done label if dl != 1
imul eax, -1 ; otherwise, we have a negative number,
; so multiply the absolute value (in EAX currently) by -1
.error:
mov eax, 4
mov ebx, 1
mov ecx, errormsg
mov edx, $-errormsg
int 80h
mov eax, 1
mov ebx, -1
int 80h
.really_done: ; Now, we are really done (for real)
pop edx
pop ebx
ret
; Input:
; EAX = integer value to convert
; ESI = pointer to buffer to store the string in (must have room for at least 10 bytes)
; Output:
; EAX = pointer to the first character of the generated string
int_to_string:
add esi,9
mov byte [esi],STRING_TERMINATOR
mov ebx,10
.next_digit:
xor edx,edx ; Clear edx prior to dividing edx:eax by ebx
div ebx ; eax /= 10
add dl,'0' ; Convert the remainder to ASCII
dec esi ; store characters in reverse order
mov [esi],dl
test eax,eax
jnz .next_digit ; Repeat until eax==0
mov eax,esi
ret
GLOBAL _start
_start:
nop ; GDB likes this
lea esi,[thestring]
mov ecx,4
call string_to_int
; EAX now contains 1234
; Convert it back to a string
lea esi,[buffer]
call int_to_string
; You now have a string pointer in EAX, which
; you can use with the sys_write system call
mov ecx, eax
mov eax, 4 ; Specify sys_write kernel call
mov ebx, 1 ; Specify file descriptor 1: Standard Output
lea ecx, [buffer]
; NOTE: ECX contains the address of the string buffer
mov edx, 10 ; buffer length
int 80h
mov eax, 1
mov ebx, 0
int 80h
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.