question in the pm tutorial

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
asmboozer

question in the pm tutorial

Post by asmboozer »

following is Pmode #1 written by Alexei A. Frounze.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PMode tutorials in C and Asm ;;
;; Copyright (C) 2000 Alexei A. Frounze ;;
;; The programs and sources come under the GPL ;;
;; (GNU General Public License), for more information ;;
;; read the file gnu-gpl.txt (originally named COPYING). ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GLOBAL _read_msw, _read_cr0, _write_cr0

SEGMENT _TEXT PUBLIC CLASS=CODE USE16

_read_msw:
smsw ax
retn

_read_cr0:
mov eax, cr0 ; read CR0 to eax
mov edx, eax
shr edx, 16 ; dx:ax = CR0 (return value)
retn

_write_cr0:
push bp
mov bp, sp
mov eax, [ss:bp+4] ; eax = 32-bit parameter
mov cr0, eax
pop bp
retn

SEGMENT _DATA PUBLIC CLASS=DATA


in the _write_cr0, to refer the first parameter, why is the offset = 4? not 8? and could I just mov eax, [bp+8]? I tihink no ss segment register needed, am I right?

when calling write_cr0, where is (E)IP register saved?is it saved in stack?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:question in the pm tutorial

Post by Candy »

asmboozer wrote: in the _write_cr0, to refer the first parameter, why is the offset = 4? not 8? and could I just mov eax, [bp+8]? I tihink no ss segment register needed, am I right?

when calling write_cr0, where is (E)IP register saved?is it saved in stack?
You're in 16-bit mode, all stack registers are 16 bits. Function before you pushes the 32-bit value. It jumps (pushes IP), push BP and then read a 32-bit value, which is 2x a 16-bit value. So, if you want the 32-bit value, read from the base + 2x 16-bit = base + 4.

Your IP is stored on the stack at BP+2. (right after the BP, which is at 0)
asmboozer

Re:question in the pm tutorial

Post by asmboozer »

I know now. but what's the difference between USE16, USE32, and their meaning? what's the default when using nasm?
Schol-R-LEA

Re:question in the pm tutorial

Post by Schol-R-LEA »

asmboozer wrote: I know now. but what's the difference between USE16, USE32, and their meaning? what's the default when using nasm?
The use16 directive is an alternate way form of the [bits 16] directive. It tells the assembler to generate 16-bit real mode code. The use32/[bits 32] directive indicates that it should generate 32-bit code.

For most cases, 32-bit p-mode code is the default. The exception to this is when assembling a program as a flat binary format (i.e., one containing only raw machine code with no linkage or relocation data), since that format is used almost exclusively in 16-bit code such as MS-DOS .com files or boot sectors (at least up to the point where it switches to p-mode, if it does). It is possible to use the directives repeatedly in a single source file, to indicate when the code generation should switch from one type to the other.
Post Reply