nasm

how to change 'jmp' and 'popfd' to 64-bit code?

▼魔方 西西 提交于 2020-01-05 05:48:05
问题 when $ nasm -f elf64 -o thisfile.o thisfile.asm it says the line of jmp and popfd "instruction not supported in 64-bit mode" this is the whole code: SELECTOR_KERNEL_CS equ 8 extern cstart extern gdt_ptr [SECTION .bss] StackSpace resb 2 * 1024 StackTop: [section .text] global _start _start: mov esp, StackTop sgdt [gdt_ptr] call cstart lgdt [gdt_ptr] ;lidt [idt_ptr] jmp SELECTOR_KERNEL_CS:csinit csinit: push 0 popfd ; Pop top of stack into EFLAGS hlt 回答1: Since you're in 64-bit mode, you'll

Exception/Error handling in NASM assembly

时光毁灭记忆、已成空白 提交于 2020-01-05 04:47:06
问题 How do I handle errors in NASM assembly? For example I have this code to read user Input: mov eax,3 mov ebx,0 mov ecx,Buffer mov edx,BUFFERLENGTH int 80H If for some reason this system call cannot be executed, I'd like to have the program jump to a label that prints "An error has occured" or something like that. How do I do that? Also, is it possible to get the name of the exception or error code? Thanks 回答1: After the kernel call, EAX is going to have two possibilites; Number of characters

Break at local label using GDB for NASM assembly

与世无争的帅哥 提交于 2020-01-04 06:14:45
问题 I want to break at a local label in NASM assembly code using GDB. Issuing the following command yields the output: (gdb) break *start.label1 + 217 Attempt to extract a component of a value that is not a structure. Here my code goes something like this: global _start _start: ... .label1: ... How can I break at local .label1 ? 回答1: Disassemble the function where your local label is called, select the address and add a break as you normally would with the address, not the label name. You can

How to force nasm to address variables position-independent?

无人久伴 提交于 2020-01-04 05:35:13
问题 How to tell NASM (or LD) to process labels in a way that will make the segment position-independent? The segment is going to be moved from one file to another and I want it to work properly on any position in any file. Here is the code that illustrates my problem: section .text ... message: db 'hello world!',0x00 ... mov rax,SYSCALL_WRITE mov rdi,STDOUT mov rsi,message mov rdx,13 syscall In the orginal executable it prints "Hello world!", but when the segment is moved to another elf, it

NASM (Intel) versus AT&T Syntax: what are the advantages?

假装没事ソ 提交于 2020-01-03 11:51:47
问题 I'm going through the Intel processor documentation and writing some basic assembly code at the same time. I have both nasm and as (GAS) on my server and I understand the basic differences of both assemblers. In the long run: Focusing on which of these syntax is a better idea? What are the advantages and disadvantages of these syntax? Which one is more widely used and understood? I would also appreciate any preferences you could share with me. 回答1: Focusing on which of these syntax is a

Using RIP-relative addressing in OSX x64 assembly

南楼画角 提交于 2020-01-02 05:18:10
问题 I was trying to make a basic printf example in x86-64 assembly code for OSX, here's my first version: section .data msg db 'hello', 0Ah section .text extern _printf global _main _main: sub rsp, 8 mov rdi, msg mov rax, 0 call _printf add rsp, 8 ret So this code is moving the absolute address of msg into rdi for the first argument to _printf , and gcc then complains about the lack of position-independent code. The binary still works though: → nasm -f macho64 new.asm && gcc -m64 -o new new.o &&

printing new lines with printf assembly

ⅰ亾dé卋堺 提交于 2020-01-01 16:19:40
问题 Hi I'm trying to write some assembly code that uses printf to print a given string. I am declaring my strings before use in the .data section and a test example looks as follows: extern printf extern fflush LINUX equ 80H ; interupt number for entering Linux kernel EXIT equ 60 ; Linux system call 1 i.e. exit () section .data outputstringfmt: db "%s", 0 sentence0: db "Hello\nWorld\n", 0 segment .text global main main: mov r8, sentence0 push r8 call print_sentence add rsp, 8 call os_return print

Develop a Bootloader In Assembly

走远了吗. 提交于 2020-01-01 13:57:53
问题 I've already done a part of my OS in Assembly, but now I want to build a own bootloader for it too instead of using GRUB. When I was developing my test OS in Assembly I remember that I boot it like this: org 0x7c00 bits 16 ; OS Kernel Here times 510 - ($-$$) db 0 dw 0xAA55 This I've already know. Now I want to use this and execute the "real" OS that will be a *.bin file written to the 2nd sector of the floppy. Then I want to know somethings How can I do a bootloader in Assembly to execute

NASM: Count how many bits in a 32 Bit number are set to 1

浪尽此生 提交于 2020-01-01 12:36:08
问题 I have a 32 Bit number and want to count know how many bits are 1. I'm thinking of this pseudocode: mov eax, [number] while(eax != 0) { div eax, 2 if(edx == 1) { ecx++; } shr eax, 1 } Is there a more efficient way? I'm using NASM on a x86 processor. (I'm just beginning with assembler, so please do not tell me to use code from extern libraries, because I do not even know how to include them ;) ) (I just found How to count the number of set bits in a 32-bit integer? which also contains my

Does [ebp*2] reference DS or SS segment?

巧了我就是萌 提交于 2020-01-01 07:56:44
问题 IDM says the memory op uses SS segment if EBP is used as base register. As a result, [ebp + esi] and [esi + ebp] references SS and DS segments, respectively. See NASM's doc: 3.3 Effective Address. In the above same section, NASM mentioned how to generate shorter machine code by replacing [eax*2] with [eax+eax] . However, NASM also generates [ebp + ebp] for [ebp*2] (i.e. no base register). I suspect [ebp+ebp] references SS segment, and [ebp*2] references DS segment. I asked NASM this question.