nasm

Isolating remainder in nasm program

感情迁移 提交于 2019-12-13 03:00:39
问题 I'm currently writing a NASM program on x86 Linux and I'm trying to perform a calculation that divides the first command line arg (a year) by the first leap year check. I want to check if the remainder is 0 or not but I'm struggling with how to do that. I know the div command stores the answer in a certain register and a remainder in another but right now I'm just using test. Here's the code global main extern puts extern printf extern atoi section .text main: sub rsp, 8 cmp rdi, 2 jne error1

Why is my register-constant comparison not working in NASM Assembly?

随声附和 提交于 2019-12-13 02:54:50
问题 Learning NASM Assembly in 32-bit Ubuntu. This is giving me headaches: suppose that I have an array (I call it vector in the program) in .data : vector db 1,2,3,4 size equ $-vector And I also have a number 0 : index db 0 I want to store size in EBX , then store index in ECX , and finally check if ECX < EBX . In this example, it would be 0 < 4 . My program simply doesn't do it: SECTION .data vector db 1,2,3,4 size equ $-vector index db 0 ; Vocabulary msg1 db "ECX < EBX",10 msg1len equ $-msg1

How can I tell if jump is absolute or relative?

落爺英雄遲暮 提交于 2019-12-13 01:05:44
问题 I'm studying for a test in assembly and in the subject of "Position-Independent-Code" I find the difference between a relative jump and an absolute jump confusing. How can I tell what kind of jump it is? I understand what a relative jump is (the offset from current line). But what does an absolute jump look like? When does it happen? 回答1: Anything that looks like just plain jmp label is relative. Absolute jumps look like jmp register jmp [address] jmp segment:absoluteaddr jmp far [address]

Relocation error when compiling NASM code in 64-bit mode

岁酱吖の 提交于 2019-12-13 00:34:04
问题 I have written a simple assembly code which I am trying to compile in 64-bit mode. Here is the code: extern printf section .rodata readinfo db `%d\n`, 0 section .text global main main: mov rbp, rsp ; for correct debugging mov rax, 5 push rax push readinfo call printf add rsp, 8 xor rax, rax mov rsp, rbp ret And here are the instructions I give to nasm and gcc (as I have read on other posts, gcc automatically links the object file with the default c libraries): nasm -f elf64 -o test.o test.asm

Assembly coding strdup. Malloc calling in shared library

自古美人都是妖i 提交于 2019-12-12 21:26:57
问题 I have a problem, I can't compile my strdup while calling malloc. When I don't call malloc, it compiles my shared library perfectly, so if someone could help me that would be great ! here is my code: BITS 64 DEFAULT REL global my_strdup:function extern malloc my_strdup: [...] call malloc I compile with this: $> nasm -f elf64 my_strdup.S $> gcc -shared -o libmy.so my_strdup.o /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: my_strdup.o: relocation R_X86_64_PC32

Load the address of a symbol (nasm, 64-bit OS X)

寵の児 提交于 2019-12-12 17:25:21
问题 I have some assembly that needs to load a C symbol in OS X (x86-64). With x86, the way you do this is: mov rax, some_symbol_name However, with x86-64, this causes a link warning: ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in _main from Test2.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie Note: I know what PIE is, and I don't want to disable it. Here are some of my other attempts to

Brackets on registers in Intel x86 assembly syntax

给你一囗甜甜゛ 提交于 2019-12-12 17:18:08
问题 I tought I understood brackets in x86 assembly. In this example, the register ax should contain X , because brackets represents the current address of LABEL . mov ax, [LABEL] LABEL: db "X", 0 But I dont understand the following two assembly lines: mov al, [ebx] Why do I need brackets? Is it because ebx is a 32 bits register and ax a 16 bits? Whats the difference with: mov al, ebx Or this one, I don't understand why I need brackets... mov [edx], ax 回答1: The bracket notation is used to let you

Why in NASM do we have to use square brackets ([ ]) to MOV to memory location?

一世执手 提交于 2019-12-12 17:18:00
问题 For example if I have a variable named test declared like: test db 0x01 ;suppose the address is 0x00000052 If I do something like: mov rax, test ;rax = 0x00000052 mov rax, [test] ;rax = 0x01 But, when I try to save in it, if we're following the same pattern: mov test, 0x01 ;address 0x00000052 = 0x01 mov [test], 0x01 ;address 0x01 = 0x01 But it actually is: mov [test], 0x01 ;address 0x00000052 = 0x01 So, why the square brackets behave differently depending if they are the first or second

When is it better for an assembler to use sign extended relocation like R_X86_64_32S instead of zero extension like R_X86_64_32?

久未见 提交于 2019-12-12 17:14:47
问题 As a concrete example, on GAS 2.24, moving the address: mov $s, %eax s: After: as --64 -o a.o a.S objdump -Sr a.o Uses zero extension: 0000000000000000 <s-0x5>: 0: b8 00 00 00 00 mov $0x0,%eax 1: R_X86_64_32 .text+0x5 But memory access: mov s, %eax s: Compiles to sign extension: 0000000000000000 <s-0x7>: 0: 8b 04 25 00 00 00 00 mov 0x0,%eax 3: R_X86_64_32S .text+0x7 Is there a rationale to using either in this specific case, or in general? I don't understand how the assembler could to any

NASM x86_64 scanf segmentation fault

梦想与她 提交于 2019-12-12 17:12:27
问题 I am new to nasm and I really want to learn how to store a number with user input. I can't get rid of getting segmentation fault when using scanf. I have searched the web, but havent found any solution to this problem. I tried this code but it doesn't work for me. Can someone explain me what am I doing wrong? global main extern printf, scanf section .data msg: db "Enter a number: ",10,0 format:db "%d",0 section .bss number resb 4 section .text main: mov rdi, msg mov al, 0 call printf push