Apply fix contributed by George Koehler:

- don't crash if BSS overlaps BDOS
- fix stack initialisation bug
- fix command line argification
This commit is contained in:
David Given 2013-06-02 22:02:15 +01:00
parent d273497077
commit aacabba165

View file

@ -15,12 +15,21 @@ MAX_ARGV = 8
.sect .bss .sect .bss
STACKSIZE = 2*1024 STACKSIZE = 2*1024
.comm stack, STACKSIZE .comm stack, STACKSIZE
.comm oldstack, 2
.sect .text .sect .text
begtext: begtext:
! The absolute first thing we have to do is to clear the bss. (argify ! Check if bss would overlap BDOS. We must not overwrite
! requires it.) ! BDOS and crash CP/M. We cheat by comparing only high bytes
! of each address.
lxi b, __end
lda 0x0007
mov c, a ! c = high byte of BDOS address
mov a, b ! a = high byte of _end
cmp c
jnc __exit ! emergency exit if a >= c
! We have to clear the bss. (argify requires it.)
lxi h, begbss lxi h, begbss
lxi b, endbss lxi b, endbss
@ -37,7 +46,7 @@ begtext:
! Set up the stack (now it's been cleared, since it's in the BSS). ! Set up the stack (now it's been cleared, since it's in the BSS).
lxi sp, oldstack + STACKSIZE lxi sp, stack + STACKSIZE
! C-ify the command line at 0x0080. ! C-ify the command line at 0x0080.
@ -54,43 +63,38 @@ begtext:
! Now argify it. ! Now argify it.
lxi b, 0x0081 ! bc = command line pointer lxi b, 0x0081 ! bc = command line pointer
lxi d, argv ! de = argv pointer lxi h, argv ! hl = argv pointer
ldax b ! peek for any leading whitespace loop_of_argify:
ora a ldax b ! a = next character
cpi ' ' ora a ! check for end of string
jz 3f jz end_of_argify
cpi ' ' ! scan for non-space
jz 2f
1: xchg ! write out the next argument mov m, c ! put next argument in argv
mov m, c inx h
inx h mov m, b
mov m, b inx h
inx h
xchg
lda argc ! exit if this was the last argument lda argc ! increment argc
inr a inr a
sta argc sta argc
cpi MAX_ARGV cpi MAX_ARGV ! exit loop if argv is full
jz end_of_argify jz end_of_argify
2: inx b ! scan for whitespace 1: inx b ! scan for space
ldax b ldax b
ora a ora a
jz end_of_argify jz end_of_argify
cpi ' ' cpi ' '
jnz 2b jnz 1b
xra a ! replace the space with a \0 xra a ! replace the space with a '\0'
stax b stax b
3: inx b ! scan for non-whitespace 2: inx b
ldax b jmp loop_of_argify
ora a
jz end_of_argify
cpi ' '
jz 3b
jmp 1b
end_of_argify: end_of_argify:
! Add the fake parameter for the program name. ! Add the fake parameter for the program name.
@ -110,7 +114,7 @@ end_of_argify:
mvi h, 0 mvi h, 0
push h push h
call __m_a_i_n call __m_a_i_n
jmp EXIT ! FALLTHROUGH
! Emergency exit routine. ! Emergency exit routine.