nasm

What is the easiest way to check an integer's remainder for modulus 2 in Nasm assembler?

风流意气都作罢 提交于 2019-12-19 11:46:31
问题 For example: int x = 35; if( x%2==1) { //do something } I just want to check the modulus value without assigning the result to x . Assume that value is in eax , so I can use DIV instruction, then put back the original value to eax etc.. but that seems inefficient. Can you suggest a better way? 回答1: To branch according to the modulo 2 of a value in al / ax / eax / rax : test al,1 jnz is_odd is_even: ; do something for even numbers. is_odd: ; do something for odd numbers. However, if all you

How to create a variable in the BSS section in NASM?

本小妞迷上赌 提交于 2019-12-19 10:23:40
问题 I tried to create a variable in the BSS section in NASM: section .bss i DD 12345 But when trying to create an object file I got the following warning: warning: attempt to initialize memory in BSS section `.bss': ignored Which is understandable I suppose since the BSS section can only contain uninitialized variables. So I attempted the following: section .bss i DD 0 But I still get the same warning. 回答1: Use RESB and friends. See the nasm manual: 3.2.2 RESB and Friends: Declaring Uninitialized

nasm calling subroutine from another file

て烟熏妆下的殇ゞ 提交于 2019-12-19 09:56:16
问题 I'm doing a project that attaches a subroutine that I wrote to a main file included by the teacher. He gave us the instructions for making our subroutine global but apparently I'm an idiot. The two asm files are in the same folder, I'm using nasm -f elf -g prt_dec.asm and ld prt_dec and then doing the same for main.asm. Here's the relevant code in the main.asm: SECTION .text ; Code section. global _start ; let loader see entry point extern prt_dec _start: mov ebx, 17 mov edx, 214123 mov edi,

dollar-terminated strings

血红的双手。 提交于 2019-12-19 09:47:58
问题 In my assembly language class, our first assignment was to write a program to print out a simple dollar-terminated string in DOS. It looked something like this: BITS 32 global _main section .data msg db "Hello, world!", 13, 10, ’$’ section .text _main: mov ah, 9 mov edx, msg int 21h ret As I understand it, the $ sign serves to terminate the sting like null does in C. But what do I do if I want to put a dollar sign in the string (like I want to print out "it costs $30")? This seems like a

Assembler Error: Mach-O 64 bit does not support absolute 32 bit addresses

偶尔善良 提交于 2019-12-19 07:49:37
问题 So I'm learning x86_64 nasm assembly on my mac for fun. After hello world and some basic arithmetic, I tried copying a slightly more advanced hello world program from this site and modifying it for 64 bit intel, but I can't get rid of this one error message: hello.s:53: error: Mach-O 64-bit format does not support 32-bit absolute addresses . Here is the command I use to assemble and link: nasm -f macho64 hello.s && ld -macosx_version_min 10.6 hello.o . And here is the relevant line: cmp rsi,

Create an exe file in assembly with NASM on 32-bit Windows

安稳与你 提交于 2019-12-19 07:33:36
问题 I'm making a hello world program in assembly language with NASM on 32-bit Windows 7 . My code is: section .text global main ;must be declared for linker (ld) main: ;tells linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db 'Hello, world!', 0xa ;our dear string len equ $ - msg ;length of

Create an exe file in assembly with NASM on 32-bit Windows

孤街浪徒 提交于 2019-12-19 07:33:12
问题 I'm making a hello world program in assembly language with NASM on 32-bit Windows 7 . My code is: section .text global main ;must be declared for linker (ld) main: ;tells linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db 'Hello, world!', 0xa ;our dear string len equ $ - msg ;length of

Why data and stack segments are executable?

给你一囗甜甜゛ 提交于 2019-12-19 05:10:40
问题 I have just noticed that my simple program has its data and stack segments executable. I saw it in /proc/[pid]/maps, and simple code confirmed it. For example: ; prog.asm section .data code: db 0xCC ;int3 section .text global _start _start: jmp code mov rax, 60 ; sys_exit mov rdi, 0 syscall then nasm -f elf64 prog.asm ld -o prog prog.o ./prog causes prog to execute int3 instruction. Programs written in C and built with gcc have their data, stack and heap non-executable, so why those written

How to use scanf in NASM?

不想你离开。 提交于 2019-12-18 19:01:02
问题 I'm trying to figure out how to use scanf to get user input. I know to use printf : all I have to do is push the data I want to write on the screen into the stack like this: global _main extern _printf extern _scanf section .data msg db "Hi", 0 section .text _main: push ebp mov ebp, esp push msg call _printf mov esp, ebp pop ebp ret But I can't figure out how to use scanf . Can someone please just give me the simplest possible source code you can for scanf ? I really just want to put what the

Get file size with stat syscall

…衆ロ難τιáo~ 提交于 2019-12-18 18:07:22
问题 I'm trying to get file size wit stat syscall with assembly (nasm): section .data encodeFile db "/home/user/file" section .bss stat resb 64 struc STAT .st_dev: resd 1 .st_ino: resd 1 .st_mode: resw 1 .st_nlink: resw 1 .st_uid: resw 1 .st_gid: resw 1 .st_rdev: resd 1 .st_size: resd 1 .st_atime: resd 1 .st_mtime: resd 1 .st_ctime: resd 1 .st_blksize: resd 1 .st_blocks: resd 1 endstruc _start: mov rax, 4 mov rdi, encodeFile mov rsi, stat syscall mov eax, dword [stat + STAT.st_size] There is 0 in