Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Minimal binary program that returns 3*4
- ;; - 102 bytes binary ELF 32-bits executable
- ;;
- ;; This assembly program is a raw binary generation of a minimal
- ;; program doing a simple task as a multiplication, and not taking
- ;; several Kilobytes for that. To prevent standard tools such as
- ;; GCC or LD to introduce unused headers and sections into the
- ;; program, we generate our custom ELF header by directly outputting
- ;; its raw binary values. This code can easily be assembled under
- ;; Linux using NASM with this command:
- ;;
- ;; $> nasm minimal_multi.asm -f bin -o mmulti
- ;;
- ;; Then it can be executed and tested as follows:
- ;;
- ;; $> chmod +x mmulti
- ;; $> ./mmulti && echo $?
- ;;
- ;; If all goes well, we shall see a 12 printed in the terminal.
- ;;
- ;; This has been possible thanks to the amazing explanations and code
- ;; from Brain Raiter. Check his site at http://www.muppetlabs.com/~breadbox/.
- ;;
- ;; LICENSE (GNU GPL v3)
- ;;
- ;; Copyright (c) 2001 Brian Raiter (http://www.muppetlabs.com/~breadbox/software/tiny/)
- ;; Copyright (C) 2021 Francisco J. Gallego-Durán (@FranGallegoBR / fjgallego@ua.es)
- ;;
- ;; This program is free software: you can redistribute it and/or modify
- ;; it under the terms of the GNU General Public License as published by
- ;; the Free Software Foundation, either version 3 of the License, or
- ;; (at your option) any later version.
- ;;
- ;; This program is distributed in the hope that it will be useful,
- ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;; GNU General Public License for more details.
- ;;
- ;; You should have received a copy of the GNU General Public License
- ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
- ;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Nasm switches to 16-bit mode when outputting raw binary data
- ;; Tell it to change to 32 bits mode. Otherwise, instructions are
- ;; incorrectly translated to machine code
- BITS 32
- ;;-------------------------------------------------------------------
- ;; WHOLE PROGRAM LOAD POINT: 0x0804800
- ;; Standard Load Point for 32bits static executables
- ;;-------------------------------------------------------------------
- org 0x08048000
- ;;-------------------------------------------------------------------
- ;; ELF HEADER (52 Bytes)
- ;;-------------------------------------------------------------------
- elf_header: ;; Elf32_Elf_Header
- db 0x7F, "ELF", 1, 1, 1, 0 ;; - e_ident
- dd 0, 0 ;;
- dw 2 ;; - e_type
- dw 3 ;; - e_machine
- dd 1 ;; - e_version
- dd _start ;; - e_entry
- dd program_header - $$ ;; - e_phoff
- dd 0 ;; - e_shoff
- dd 0 ;; - e_flags
- dw ehdr_size ;; - e_ehsize
- dw phdr_size ;; - e_phentsize
- dw 1 ;; - e_phnum
- dw 0 ;; - e_shentsize
- dw 0 ;; - e_shnum
- dw 0 ;; - e_shstrndx
- ehdr_size equ ($ - elf_header) ;; Size of Elf Header
- ;;-------------------------------------------------------------------
- ;; PROGRAM HEADER (32 bytes)
- ;;-------------------------------------------------------------------
- program_header: ;; Elf32_Program_Header
- dd 1 ;; - p_type
- dd 0 ;; - p_offset
- dd $$ ;; - p_vaddr
- dd $$ ;; - p_paddr
- dd filesize ;; - p_filesz
- dd filesize ;; - p_memsz
- dd 5 ;; - p_flags
- dd 0x1000 ;; - p_align
- phdr_size equ ($ - program_header) ;; Size of the Program Header
- ;;-------------------------------------------------------------------
- ;; PROGRAM
- ;;-------------------------------------------------------------------
- _start:
- mov ebx, 3 ;; EBX = 3
- imul ebx, 4 ;; EBX = 4*EBX
- mov eax, 1 ;; EAX = 1 ( Return to system sys_call, with EBX as return value )
- int 0x80 ;; System Call
- ;;-------------------------------------------------------------------
- ;; SIZE OF THIS WHOLE FILE
- ;;-------------------------------------------------------------------
- filesize equ ($ - $$)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement