Re: Transfering control from 32bit to 16bit application

From: flekso (taurus_at_email.hinet.hr)
Date: 10/31/03


Date: Fri, 31 Oct 2003 10:41:25 +0100


"Davor Grguric" <davor.grgruric@ri.hinet.hr> wrote in message
news:bns48b$sqj$1@ls219.htnet.hr...
> Thanks,
> I'm going to study this stuff.It seeams that this is what i need. Do you
> have any sources.
> Regards,
> Davor
>
>

here's an example from ' ia-32 intel architecture software developer's
manual, volume 3: system programming guide ' :
( http://www.intel.com/design/Pentium4/manuals/245472.htm )

NOTE

This code is listed as ASM386 assembly code. However, this code is

compatible with all IA-32 processors from the Intel386 processors through

the Intel486, Pentium, P6 family, Intel Xeon, and Pentium 4 processors; that

is, once assembled, this code will execute as expected on all IA-32
processors

beginning with the Intel386 processor.

Example 8-1. STARTUP.ASM

MS-DOS* 5.0(045-N) 386(TM) MACRO ASSEMBLER STARTUP 09:44:51 08/19/92

PAGE 1

MS-DOS 5.0(045-N) 386(TM) MACRO ASSEMBLER V4.0, ASSEMBLY OF MODULE

STARTUP

OBJECT MODULE PLACED IN startup.obj

ASSEMBLER INVOKED BY: f:\386tools\ASM386.EXE startup.a58 pw (132 )

LINE SOURCE

1 NAME STARTUP

2

3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

4 ;

5 ; ASSUMPTIONS:

6 ;

7 ; 1. The bottom 64K of memory is ram, and can be used for

8 ; scratch space by this module.

9 ;

10 ; 2. The system has sufficient free usable ram to copy the

11 ; initial GDT, IDT, and TSS

12 ;

13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

14

15 ; configuration data - must match with build definition

16

17 CS_BASE EQU 0FFFF0000H

18

19 ; CS_BASE is the linear address of the segment STARTUP_CODE

20 ; - this is specified in the build language file

21

22 RAM_START EQU 400H

23

24 ; RAM_START is the start of free, usable ram in the linear

25 ; memory space. The GDT, IDT, and initial TSS will be

26 ; copied above this space, and a small data segment will be

27 ; discarded at this linear address. The 32-bit word at

28 ; RAM_START will contain the linear address of the first

29 ; free byte above the copied tables - this may be useful if

30 ; a memory manager is used.

31

32 TSS_INDEX EQU 10

33

34 ; TSS_INDEX is the index of the TSS of the first task to

35 ; run after startup

36

37

38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

39

40 ; ------------------------- STRUCTURES and EQU ---------------

41 ; structures for system data

42

43 ; TSS structure

44 TASK_STATE STRUC

45 link DW ?

46 link_h DW ?

47 ESP0 DD ?

48 SS0 DW ?

49 SS0_h DW ?

50 ESP1 DD ?

51 SS1 DW ?

52 SS1_h DW ?

53 ESP2 DD ?

54 SS2 DW ?

55 SS2_h DW ?

56 CR3_reg DD ?

57 EIP_reg DD ?

58 EFLAGS_reg DD ?

59 EAX_reg DD ?

60 ECX_reg DD ?

61 EDX_reg DD ?

62 EBX_reg DD ?

63 ESP_reg DD ?

64 EBP_reg DD ?

65 ESI_reg DD ?

66 EDI_reg DD ?

67 ES_reg DW ?

68 ES_h DW ?

69 CS_reg DW ?

70 CS_h DW ?

71 SS_reg DW ?

72 SS_h DW ?

73 DS_reg DW ?

74 DS_h DW ?

75 FS_reg DW ?

76 FS_h DW ?

77 GS_reg DW ?

78 GS_h DW ?

79 LDT_reg DW ?

80 LDT_h DW ?

81 TRAP_reg DW ?

82 IO_map_base DW ?

83 TASK_STATE ENDS

84

85 ; basic structure of a descriptor

86 DESC STRUC

87 lim_0_15 DW ?

88 bas_0_15 DW ?

89 bas_16_23 DB ?

90 access DB ?

91 gran DB ?

92 bas_24_31 DB ?

93 DESC ENDS

94

95 ; structure for use with LGDT and LIDT instructions

96 TABLE_REG STRUC

97 table_lim DW ?

98 table_linear DD ?

99 TABLE_REG ENDS

100

101 ; offset of GDT and IDT descriptors in builder generated GDT

102 GDT_DESC_OFF EQU 1*SIZE(DESC)

103 IDT_DESC_OFF EQU 2*SIZE(DESC)

104

105 ; equates for building temporary GDT in RAM

106 LINEAR_SEL EQU 1*SIZE (DESC)

107 LINEAR_PROTO_LO EQU 00000FFFFH ; LINEAR_ALIAS

108 LINEAR_PROTO_HI EQU 000CF9200H

109

110 ; Protection Enable Bit in CR0

111 PE_BIT EQU 1B

112

113 ; ------------------------------------------------------------

114

115 ; ------------------------- DATA SEGMENT----------------------

116

117 ; Initially, this data segment starts at linear 0, according

118 ; to the processor's power-up state.

119

120 STARTUP_DATA SEGMENT RW

121

122 free_mem_linear_base LABEL DWORD

123 TEMP_GDT LABEL BYTE ; must be first in segment

124 TEMP_GDT_NULL_DESC DESC <>

125 TEMP_GDT_LINEAR_DESC DESC <>

126

127 ; scratch areas for LGDT and LIDT instructions

128 TEMP_GDT_SCRATCH TABLE_REG <>

129 APP_GDT_RAM TABLE_REG <>

130 APP_IDT_RAM TABLE_REG <>

131 ; align end_data

132 fill DW ?

133

134 ; last thing in this segment - should be on a dword boundary

135 end_data LABEL BYTE

136

137 STARTUP_DATA ENDS

138 ; ------------------------------------------------------------

139

140

141 ; ------------------------- CODE SEGMENT----------------------

142 STARTUP_CODE SEGMENT ER PUBLIC USE16

143

144 ; filled in by builder

145 PUBLIC GDT_EPROM

146 GDT_EPROM TABLE_REG <>

147

148 ; filled in by builder

149 PUBLIC IDT_EPROM

150 IDT_EPROM TABLE_REG <>

151

152 ; entry point into startup code - the bootstrap will vector

153 ; here with a near JMP generated by the builder. This

154 ; label must be in the top 64K of linear memory.

155

156 PUBLIC STARTUP

157 STARTUP:

158

159 ; DS,ES address the bottom 64K of flat linear memory

160 ASSUME DS:STARTUP_DATA, ES:STARTUP_DATA

161 ; See Figure 8-4

162 ; load GDTR with temporary GDT

163 LEA EBX,TEMP_GDT ; build the TEMP_GDT in low ram,

164 MOV DWORD PTR [EBX],0 ; where we can address

165 MOV DWORD PTR [EBX]+4,0

166 MOV DWORD PTR [EBX]+8, LINEAR_PROTO_LO

167 MOV DWORD PTR [EBX]+12, LINEAR_PROTO_HI

168 MOV TEMP_GDT_scratch.table_linear,EBX

169 MOV TEMP_GDT_scratch.table_lim,15

170

171 DB 66H ; execute a 32 bit LGDT

172 LGDT TEMP_GDT_scratch

173

174 ; enter protected mode

175 MOV EBX,CR0

176 OR EBX,PE_BIT

177 MOV CR0,EBX

178

179 ; clear prefetch queue

180 JMP CLEAR_LABEL

181 CLEAR_LABEL:

182

183 ; make DS and ES address 4G of linear memory

184 MOV CX,LINEAR_SEL

185 MOV DS,CX

186 MOV ES,CX

187

188 ; do board specific initialization

189 ;

190 ;

191 ; ......

192 ;

193

194

195 ; See Figure 8-5

196 ; copy EPROM GDT to ram at:

197 ; RAM_START + size (STARTUP_DATA)

198 MOV EAX,RAM_START

199 ADD EAX,OFFSET (end_data)

200 MOV EBX,RAM_START

201 MOV ECX, CS_BASE

202 ADD ECX, OFFSET (GDT_EPROM)

203 MOV ESI, [ECX].table_linear

204 MOV EDI,EAX

205 MOVZX ECX, [ECX].table_lim

206 MOV APP_GDT_ram[EBX].table_lim,CX

207 INC ECX

208 MOV EDX,EAX

209 MOV APP_GDT_ram[EBX].table_linear,EAX

210 ADD EAX,ECX

211 REP MOVS BYTE PTR ES:[EDI],BYTE PTR DS:[ESI]

212

213 ; fixup GDT base in descriptor

214 MOV ECX,EDX

215 MOV [EDX].bas_0_15+GDT_DESC_OFF,CX

216 ROR ECX,16

217 MOV [EDX].bas_16_23+GDT_DESC_OFF,CL

218 MOV [EDX].bas_24_31+GDT_DESC_OFF,CH

219

220 ; copy EPROM IDT to ram at:

221 ; RAM_START+size(STARTUP_DATA)+SIZE (EPROM GDT)

222 MOV ECX, CS_BASE

223 ADD ECX, OFFSET (IDT_EPROM)

224 MOV ESI, [ECX].table_linear

225 MOV EDI,EAX

226 MOVZX ECX, [ECX].table_lim

227 MOV APP_IDT_ram[EBX].table_lim,CX

228 INC ECX

229 MOV APP_IDT_ram[EBX].table_linear,EAX

230 MOV EBX,EAX

231 ADD EAX,ECX

232 REP MOVS BYTE PTR ES:[EDI],BYTE PTR DS:[ESI]

233

234 ; fixup IDT pointer in GDT

235 MOV [EDX].bas_0_15+IDT_DESC_OFF,BX

236 ROR EBX,16

237 MOV [EDX].bas_16_23+IDT_DESC_OFF,BL

238 MOV [EDX].bas_24_31+IDT_DESC_OFF,BH

239

240 ; load GDTR and IDTR

241 MOV EBX,RAM_START

242 DB 66H ; execute a 32 bit LGDT

243 LGDT APP_GDT_ram[EBX]

244 DB 66H ; execute a 32 bit LIDT

245 LIDT APP_IDT_ram[EBX]

246

247 ; move the TSS

248 MOV EDI,EAX

249 MOV EBX,TSS_INDEX*SIZE(DESC)

250 MOV ECX,GDT_DESC_OFF ;build linear address for TSS

251 MOV GS,CX

252 MOV DH,GS:[EBX].bas_24_31

253 MOV DL,GS:[EBX].bas_16_23

254 ROL EDX,16

255 MOV DX,GS:[EBX].bas_0_15

256 MOV ESI,EDX

257 LSL ECX,EBX

258 INC ECX

259 MOV EDX,EAX

260 ADD EAX,ECX

261 REP MOVS BYTE PTR ES:[EDI],BYTE PTR DS:[ESI]

262

263 ; fixup TSS pointer

264 MOV GS:[EBX].bas_0_15,DX

265 ROL EDX,16

266 MOV GS:[EBX].bas_24_31,DH

267 MOV GS:[EBX].bas_16_23,DL

268 ROL EDX,16

269 ;save start of free ram at linear location RAMSTART

270 MOV free_mem_linear_base+RAM_START,EAX

271

272 ;assume no LDT used in the initial task - if necessary,

273 ;code to move the LDT could be added, and should resemble

274 ;that used to move the TSS

275

276 ; load task register

277 LTR BX ; No task switch, only descriptor loading

278 ; See Figure 8-6

279 ; load minimal set of registers necessary to simulate task

280 ; switch

281

282

283 MOV AX,[EDX].SS_reg ; start loading registers

284 MOV EDI,[EDX].ESP_reg

285 MOV SS,AX

286 MOV ESP,EDI ; stack now valid

287 PUSH DWORD PTR [EDX].EFLAGS_reg

288 PUSH DWORD PTR [EDX].CS_reg

289 PUSH DWORD PTR [EDX].EIP_reg

290 MOV AX,[EDX].DS_reg

291 MOV BX,[EDX].ES_reg

292 MOV DS,AX ; DS and ES no longer linear memory

293 MOV ES,BX

294

295 ; simulate far jump to initial task

296 IRETD

297

298 STARTUP_CODE ENDS

*** WARNING #377 IN 298, (PASS 2) SEGMENT CONTAINS PRIVILEGED

INSTRUCTION(S)

299

300 END STARTUP, DS:STARTUP_DATA, SS:STARTUP_DATA

301

302



Relevant Pages

  • Re: vesa linear framebuffer
    ... be doing that for every pixel. ... That won't get you "hardware acceleration", ... The linear framebufffer don´t need a bank switching. ... mov di, OFFSET VINF ...
    (alt.lang.asm)
  • Re: about nasm copy memory to memory problem
    ... mov ax,INITSEG ... sub si,si; ... which is linear address 7C000. ... xor di,di ...
    (comp.lang.asm.x86)
  • Re: Fastcode MM B&V 0.39
    ... mov ecx, ... mov [edx], ecx ... NewSize: Integer; ...
    (borland.public.delphi.language.basm)
  • Re: Fastcode MM B&V 0.41
    ... mov ecx, ... mov [edx], ecx ... NewSize: Integer; ...
    (borland.public.delphi.language.basm)
  • Memory Managers for Beginners 5
    ... mov ecx, ... function GrowAllocArray(var AllocArray: PAllocTypeArray; var AllocArraySize ...
    (borland.public.delphi.language.basm)