Last active
June 20, 2021 04:10
-
-
Save riicchhaarrd/a9b48b95fa30a7727856ec32d2bed98a to your computer and use it in GitHub Desktop.
linux x86 nasm assembly print out decimal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global _start | |
| BITS 32 | |
| section .data | |
| string: db "Value is ", 0 | |
| newline_string: db 10, 0 | |
| section .text | |
| strlen: | |
| push ebp | |
| mov ebp, esp | |
| xor eax,eax | |
| mov ecx,[ebp+8] | |
| loop: | |
| xor dx,dx | |
| mov dl,byte [ecx+eax] | |
| inc eax | |
| cmp dl,0x0 | |
| jne loop | |
| end: | |
| mov esp,ebp | |
| pop ebp | |
| ret | |
| print_string: | |
| push ebp | |
| mov ebp, esp | |
| mov ebx,1 ; use stdout | |
| mov ecx,[ebp + 8] ; first arg pushed as string | |
| push ecx | |
| call strlen | |
| mov edx,eax | |
| mov eax,4 | |
| int 0x80 | |
| mov esp,ebp | |
| pop ebp | |
| ret | |
| print_dec: | |
| push ebp | |
| mov ebp, esp | |
| mov eax,[ebp + 8] ;byte arg | |
| sub esp, 32 | |
| mov esi, 0 | |
| lea ecx, [ebp] | |
| mov byte [ecx], 0 | |
| l1: | |
| ;; cdq | |
| xor edx,edx | |
| mov ebx,10 | |
| idiv ebx | |
| ;; remainder is in edx | |
| add dl,'0' | |
| lea ecx, [ebp - 1] | |
| sub ecx, esi | |
| mov byte [ecx], dl | |
| inc esi | |
| test eax,eax | |
| jnz l1 | |
| lea ecx, [ebp] | |
| sub ecx, esi | |
| push ecx | |
| call print_string | |
| add esp, 4 | |
| mov esp,ebp | |
| pop ebp | |
| ret | |
| _start: | |
| mov eax, 10 | |
| mov ecx, 123 | |
| xor edx,edx | |
| imul ecx | |
| push eax | |
| call print_dec | |
| add esp, 4 | |
| mov ebx,0 | |
| xor eax,eax | |
| mov al,1 | |
| ;; mov ebx,123 | |
| int 0x80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment