Your problem is that the program counter is 512 when it reaches the code for the second sector. This means the address for v2 is 512 bytes higher than where it appears in memory.
One way to fix it:
Code: Select all
times 510 - ($ - $$) db 0
dw 0xAA55
SecondSectorBase equ $
; Second code
jmp main
main:
mov ax, 0x1000
mov ds, ax
mov es, ax
cli
mov ax, 0x7000
mov ss, ax
mov sp, ss
sti
mov ah, 0x0E
mov al, 'B'
int 0x10
mov ah, 0x0E
mov al, [v2 - SecondSectorBase]
int 0x10
; BOCHS will warn you about executing HLT when IF = 0, so you'll know when you've reached here
; Doing info registers in the Qemu monitor will tell you HLT=1 aswell
; This will help you know what has been executed and can act as a rudimentry breakpoint.
cli
hlt
v2: db 'C'
times 1024 - ($ - $$) db 0
You'd have to do "var - SecondSectorBase" anytime you accessed a variable though, so its not suited well for a largish code.
Another way to do it at run time is to set your data\extra segments to 0xFE0 rather than 0x1000, which would 'automagically' subtract 512 from all your variables for you. This is a very ugly\confusing hack though.
The proper (IMO) way to do this is to just load the second sector immediately after the first. This has several advantages, namely you don't have to reset the segment registers after jumping to the second sector. The program counter will also be good, and you can easily access code that lies in the first sector.
Complete Example:
Code: Select all
%define sec 2
[bits 16]
[org 0x0000]
jmp 0x7C0:load
load:
mov ax, cs
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
xor ax, ax
mov ss, ax
mov sp, 0x7C00 ; You need a stack before you call the BIOS
mov al, [v1]
mov ah, 0x0E
int 0x10
call loados
jmp main
loados: ; Load 1 sector
mov bx, 0x200 ; 0x7E00. Remember ES is already set
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, sec
mov dh, 0x00
int 0x13
jc error
ret
error:
mov esp, 0xDEADDEAD
cli
hlt
v1: db 'A'
times 510 - ($ - $$) db 0
dw 0xAA55
; Second code
main:
mov ah, 0x0E
mov al, 'B'
int 0x10
mov ah, 0x0E
mov di, v2
mov al, [v2]
int 0x10
cli
hlt
v2: db 'C'
times 1024 - ($ - $$) db 0