assembly,operating-system,gas,protected-mode , Protected mode, setting segment registers

Protected mode, setting segment registers


Tag: assembly,operating-system,gas,protected-mode

I'm recently playing with gnu-assembler in simple os development. I'm using code below to switch CPU to protected mode. In order to do this I set GDT as follow and performed far jump to given label wit 0x08 as GDT offset (to set CS). CPU did not reset itself after jmp, but none of mov instructions were executed correctly after jumping to leaveToKernel. The reason why Im saying that mov failed to set DS and SS is this qemu print (info registers):

EAX=00000000 EBX=00105fd8 ECX=000003eb EDX=000b8000
ESI=00010000 EDI=00000000 EBP=00105fc0 ESP=00105fc0
EIP=0083ec44 EFL=00200002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0018 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00100000 02710fff 00c09a00 DPL=0 CS32 [-R-]
SS =0018 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0018 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0018 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0018 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]

Can someone help me understand and explain this behaviour? Thanks in advance

The code used to switch CPU to p-mode(GAS syntax):

.equ NULL_DESCRIPTOR,  0x0000000000000000

.equ CODE0,          0x00C09A1000002710
.equ DATA0,          0x00C09210010007D0
.equ PLACE_HLD1,     0x0000000000000000
.equ PLACE_HLD2,     0x0000000000000000
.section .data
        .quad NULL_DESCRIPTOR
        .quad CODE_P0
        .quad DATA_P0
        .quad PLACE_HLD1
        .quad PLACE_HLD2
        .word 24
        .long GDT
.section .text
.global setProtectedMode
.type setProtectedMode, @function
    push %ebp
    mov %esp,%ebp

    lgdt _GDT
    mov %cr0, %eax
    or $1, %eax
    mov %eax, %cr0
    jmp $0x08 ,$leaveToKernel
    xor %eax, %eax
    mov $0x10, %ax
    mov %ax, %ss
    mov %ax, %ds


Apparently your CS base is not zero so I say you are not jumping to the proper place. It's not usual to have non-zero CS base, I assume you did want 0, but set up the GDT entry wrong. Fix it :)


How is a file loaded in the FAT file system?

I am developing a bootloader in x86 Assembly and I have a question about the FAT file system. Which steps are necessary to find and load the file?...

Counter not working after jumps - assembly language

For some reason, when i switch to mouse input switch back to keyboard input for my program, increasing and decreasing the counter has no effect. It works perfectly in the first loop where we input characters. Here is the program guys, any advice? look at whatspeed jump for reference after...

MinGW's ld cannot perform PE operations on non PE output file

I know there are some other similar questions about this out there, be it StackOverflow or not. I've researched a lot for this, and still didn't find a single solution. I'm doing an operative system as a side project. I've been doing all in Assembly, but now I wanna join...

Not Used Recently (NUR) Page Replacement Algorithm [closed]

How to implement NUR (Not used recently page replacement algorithm) using any high level programming language (c, c++ or java)

How to represent mips instruction as it's hex representation

I'm given a MIPS instruction: top: lw $t1, ($t0) beq $s0, $0, end jal func add $s0, $s0, $t0 addi $s0, $s0, -1 j top bne $s0, $0, top end: func: sll $v0, $t1, 4 jr $ra and am told to convert each line to the "instruction in hex." What...

Is this an overflow, or maybe more keyboard data?

I am writing a bootloader, and it's functionality is basically limited to printing a string, then copying keyboard characters to the screen as they are typed. While writing the routines that read and write the key, I noticed my print routine was not detecting the null terminator in offset (plus)...

How to jump to an address saved in a register in intel assembly? [duplicate]

This question already has an answer here: conditional jump to register 1 answer say I calculated an address of a label and stored it in eax register, how can I JMP(specifically JE) to eax? jmp eax compiles, but I didn't check if it works. je eax doesn't compile(invalid combination...

Difference between switch & bus architecture?

I was just going through a operating systems textbook but I got stuck at switch architecture . Please explain what it is and how it is different from bus architecture ?...

Print string using INT 0x10 in bootsector

I want to create printl function that allow me to print string in the ax register. I am in 16-bit real mode and I can not find any way to print a message. I using int 0x10 to print a single letter. I try pass argument (string to print) in...

LC3 assembly-unable to print the right character

I have been trying to make a program that decrypts a text that is being read from a file under the following condiditons: The adress of the first element of the file is 5001.Each letter is encrypted twice:The first time with Ceasar encryption and the second time with XOR encryption.The...

How do I check assembly output of Java code?

I found this question that answered it for C++: How do you get assembler output from C/C++ source in gcc?

How to detect that music play in background

I want help in detecting if there is a music play in background on the phone or not because I play more than one music sequentially in my app but they are intersection thank you

GCC emits vastly different code using “-march=native” on similar architectures

I'm working on writing an OpenCL benchmark in C. Currently, it measures the fused multiply-accumulate performance of both a CL device, and the system's processor using C code. The results are then cross checked for accuracy. I wrote the native code to take advantage of GCC's auto vectorizer, and it...

nasm: jump when input is NULL

I am recoding puts using nasm (64bit), and when puts receives NULL as argument it prints (null). I'm trying to recreate that behaviour, except I can't get the code to jump to the part where it prints (null). Instead it just prints nothing here's my code: global _my_puts section .text...

storing array from user and accessing it

I have to create an int array based on user input, sum the integers, and output the sum and the array itself. My code calculates the sum correctly, but I cannot output the list. It only prints out the last number and zeros and then gets stuck in an infinite...

assembly function with C segfault

I am trying to make assembly function that uses SSE and FPU for parallel calculations. Unfortunately I am receiving segmentation fault(core dumped) error(while debugging it doesn't show in assembly function). I also cannot step out from assembly function. Gdb shows: Warning: Cannot insert breakpoint 0. Cannot access memory at address...

Why can't I use compiler intrinsics in an asm block?

Is this a compiler bug? program Project44; {$APPTYPE CONSOLE} uses System.SysUtils; function Test: integer; asm xor eax,eax add eax,SizeOf(NativeInt); end; begin WriteLn(Test); //Typically outputs 31 or 49 {$ifdef CPUX86} WriteLn('should be 4'); {$else} {$ifdef CPUX64} WriteLn('should be 8'); {$endif}{$endif} ReadLn end. This program outputs all kinds of things, except for...

Visual Studios building and debugging .cpp file without main()

Professor just gave us a block of c++ code in order to learn about the debug windows in Visual Studio, however after creating a blank win32 console project and then dragging in the .cpp file I get the missing .exe error message when attempting to debug. I've looked everywhere and...

Print a number in NASM - building an x86 Bootsector

I just started messing around with assembly language and I tried to print the number 9 on console. Here is what I wrote: global _main section .data digit equ 9 section .bss section .text _main: mov edx, 1 mov ecx, digit add ecx, 48 mov ebx, 1 mov eax, 4...

subl causing Floating point exception

I am creating a compiler which should compile a fictional language into asm x86 code. When compiling this piece of code (fictional code): int x; int f(int n) { write n; } int main() { x = 1; f(x); } write is equal to just print in console. The desired...

How does this instruction look in memory?

I am having trouble figuring out how this instruction looks in memory for a x86 processor. mov $0x3c,%eax Can someone help me figure it out? For example an easy one is: xor %edi,%edi ---> 0x31 0xFF...

Declaring Variables in the .data Versus on the Stack - ASM

I'm trying to understand in which cases you would want to declare variables in the .data section of my assembly program and using it for the procedures needed, over instantiating local variables on the stack and vice versa. Is it just that declaring variables in the .data section is only...

How is shellcode generated from C? - With code example

I started taking an online course regarding software security. In one of the sections, I was instructed to execute a hidden C function using a buffer overflow. I got to thinking: what would happen if I could pass machine instruction directly to a stack unsafe executable? What I have been...

How can I access the individual elements of an array in a loop?

I need to print the cells of an array, I have an array which contains the word "HELLO_WORLD", I manage to print an index by its own but I can't manage to print all the cells one by one, here is the code : loop: la $t0, hexdigits # address...

Understanding NASM Macro

I've come around this macro in a assembly source file and I just can't figure out how it's working. So first I come around this function (hevc_deblock.h): cglobal hevc_v_loop_filter_chroma_8, 3, 5, 7, pix, stride, tc, pix0, r3stride sub pixq, 2 lea r3strideq, [3*strideq] mov pix0q, pixq add pixq, r3strideq TRANSPOSE4x8B_LOAD...

Automate MySQL backup @localhost with mysqldump in Windows 8

I'm trying to set up a task in Windows 8 to automate my localhost db dump. I've created the task to run daily with the following command line: C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe and parameters: --user=root --password=donttellya --result-file=dumped.sql mydb It works but doesn't terminate (running in the tasks list). Also, I'd...

Make the input wait for mouse or keyboard - Assembly Language

I fixed my program, but now the loop seems to be stuck. Whenever I press u or d, it is stuck, but the mouse part works :) ; You may customize this and other start-up templates; ; The location of this template is c:\emu8086\inc\0_com_template.txt org 100h CLEAR MACRO MOV AX,0600H...

LC3 assembly-how to count string length

I am trying to create a program at LC3 assembly that counts the length of a string in the following way: All data is already stored somewhere in memory. There is a variable in which the address of the first element of the string is stored. (I apologize in advance...

NASM: Makefile for library

I'm having trouble building a makefile for a library in nasm, since it requires that you run nasm with one input file at a time. I have tried with the %.o : %.s thing but I'm probably doing it incorrectly since it's not working. Here is what I have: NAME...

Perform integer division using multiplication

Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) / 0x100000000 For example, performing a division by 9: 12345678 / 9...

Asm x86 segmentation fault in reading from file

I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong? .global _start _start: mov $2,%rax mov 16(%rsp),%rdi mov $02,%rsi syscall cmp $0,%rax jl exit 1:...

How my computer knows a char corresponds to which character?

Does my OS load a font file which maps the UTF-8 codes to characters(the way to render it ) when I start up my computer? Since there are a lot of different fonts, the font files will consume a lot of memory? Or the font files are stored in hard-disk...

ARM assembly cannot use immediate values and ADDS/ADCS together

I am currently trying to speed up some of my C functions on a Cortex-M0 (Freescale KL25Z) using assembly. I get a problem with this minimal test program: @.syntax unified .cpu cortex-m0 .text .global test .code 16 test: mov r0, #0 adds r0, r0, #1 bx lr When I try...

Macro to push arguments onto stack

I've been working on a fun little project to mess around with how functions are called and I need a macro to push arguments as it'll be quite time consuming to push the arguments manually for every instance of this obfuscated call. This is my code so far: #define pushargs(...)...

MASM SEG operator

Let's assume we are writing assembly code using MASM 6.1 / 16 bit / huge data model, and we have a variable (label) named MY_VAR, a segment named MY_SEG and a segment group named MY_GROUP. Let's assume MY_VAR is placed in MY_SEG, and MY_SEG belongs to MY_GROUP. Then, what is...

NSUserDefaults not working on Xcode beta with Watch OS2

I just installed the latest beta of Xcode to try Swift 2 and the improvements made to the Apple Watch development section. I'm actually having an hard time figuring out WHY this basic NSUserDefaults method to share informations between iOS and Watch OS2 isn't working. I followed this step-by-step tutorial...

Range of Addresses for a Conditional Branch Instruction in MIPS

What is the range of addresses for a conditional branch instruction in MIPS with respect to X, where X contains the address of the branch instruction? Assume the current PC value is 0x10000000. What is the range of addresses for jump (j) and the jump and link (jal) in- structions...

Open Watcom Inline assembly SEG and OFFSET operators

Today, I have learned that the SEG operator in MASM by default returns the address of the GROUP and not the address of the SEGMENT of the expression in question, and that there are options and methods to override that. Since I am currently doing a complex project in Open...

Range of immediate values in ARMv8 A64 assembly

My understanding is that immediate parameters in ARMv8 A64 assembly can be 12 bits long. If that is the case, why does this line of assembly code: AND X12, X10, 0xFEF Produce this error (when compiled with gcc) Error: immediate out of range at operand 3 -- `AND X12, X10,...

Wrong answer from DIV assembly

I have a part of my code mov di,3 mov cx,16 looop: xor dx,dx shl bx,1 adc dx,dx cmp cx,16 je cx16 (it's will dec cx and jump loop) push dx dec cx cmp cx,0 je cx0 mov ax,cx div di cmp dx,0 jne looop when cx = 3 i...

NASM: copying a pointer from a register to a buffer in .data

I am new to asm. I am trying to copy a pointer from a register to a .data variable using NASM, on linux 64-bit. Concider this program: section .data ptr: dq 0 section .text global _start _start: mov [ptr], rsp mov rax, 60 mov rdi, 0 syscall Here I try...

Why Masm32 only give 1 to 100 result for add and subtraction operation and beyond that I got wrong answer?

I am new to assembly language. I make this code just now and run it with no errors so far, except It will only give a result from 1 - 100, here's my code. This is a simple math operation which is addition and subtraction. I tried analyzing the code...

How to change ubuntu to root?

I am new to linux. when i list the files under folder 1, i see below result -rwxrwxrwx 1 root root 920 Jun 3 10:36 file1.xsd -rwxrwxrwx 1 root root 920 Jun 3 10:36 file2.xsd drwxrwxrwx 2 ubuntu ubuntu 4096 Jun 12 11:10 temp -rwxrwxrwx 1 ubuntu ubuntu 1853 Jun...

What does DX + 2 mean in mov ah,9 int 21h?

mov dx, offset buffer mov ah, 0ah int 21h jmp print buffer db 10,?, 10 dup(' ') print: xor bx, bx mov bl, buffer[1] mov buffer[bx+2], '$' mov dx, offset buffer + 2 mov ah, 9 int 21h I know buffer[bx+2] stands for '$', but offset buffer +2 in mov...

Reserve bytes in stack: x86 Assembly (64 bit)

pushq %rbp movq %rsp, %rbp subq $32, %rsp I have big question regarding explanation of "$32" in third instruction. The information from search and blogs specifies that in above third instruction we are reserving certain "bytes" of space for stack. From doc, %rsp is 64 bit register and %esp is...

^M behind operating system version?

I am currently writing a perl script to extract data from a log file and create statements using the data in a new file. In this new file, one particular data that I've extracted and used here is the operating system's version. It shows Windows XP Service Pack 3^M. I...

x86 jmp asterisk %eax

In the paper for Google Native Client, the authors define a nacljmp as these two instructions: and %eax, 0xffffffe0 // Clears the 5 least significant bits in %eax. jmp *%eax First, I can see they clear the 5 least signficiant bits to make the instruction 32 bit aligned before it...

NASM assembled bootloader memory issue

I am writing a bootloader with nasm. At the moment it is designed to output a welcome string, then record keystrokes while displaying them, printing the stored keystrokes upon finding the enter key, and finally halting. bits 16 org 0x7C00 start: jmp main bgetkey: pusha mov ax, 0 mov ah,...

Trouble outputing file size to a label from a listbox in Python 3

I'm using os.path.getsize() to output the size of a file to a label. The file path is stored in a listbox. The function works, but it outputs the file size in bits, so I wrote the following to convert to more appropriate units, and it is now displaying only in...

Error: Junk at EOL, first unrecognised character is '('

I am trying to compile Quake 1 (QW) from source code and have run into the following problem when compiling client/d_copy.s : Error: Junk at EOL, first unrecognised character is '('. The file in question is here (please excuse the syntax highlighting, pastebin only supports NASM). I am attempting to...