wok diff linld/stuff/src/CRTL.ASM @ rev 19538
linld: add 'linld <kernel> <cmdline>' syntax
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Dec 02 12:37:59 2016 +0100 (2016-12-02) |
parents | bb42796dcd3b |
children | 31c5cbbd9380 |
line diff
1.1 --- a/linld/stuff/src/CRTL.ASM Tue Nov 22 21:19:01 2016 +0100 1.2 +++ b/linld/stuff/src/CRTL.ASM Fri Dec 02 12:37:59 2016 +0100 1.3 @@ -13,8 +13,7 @@ 1.4 segment _DATA byte public use16 'DATA' 1.5 1.6 msg_hang db "High mem corrupted - not exiting to DOS",0 1.7 - global _vcpi_alloc_err:byte 1.8 -_vcpi_alloc_err db "vcpi " 1.9 +vcpi_alloc_err db "vcpi " 1.10 msg_malloc db "malloc() failure",0 1.11 msg_crlf db 13,10,0 1.12 1.13 @@ -34,6 +33,104 @@ 1.14 segment _TEXT byte public use16 'CODE' 1.15 1.16 ;*************************************************************** 1.17 +;char* strcpy(const char* a, const char* b); 1.18 +;*************************************************************** 1.19 + global _strcpy:near 1.20 + proc _strcpy near 1.21 + 1.22 + mov dl,0 1.23 +cat: 1.24 + pop ax ;caller return address 1.25 + pop cx ; a 1.26 + pop bx ; b 1.27 + push bx 1.28 + push cx 1.29 + push ax 1.30 + push si 1.31 + mov si,cx 1.32 + shr dl,1 1.33 + jnc @@nocat 1.34 +@@catlp: 1.35 + lodsb 1.36 + cmp al,0 1.37 + jne @@catlp 1.38 + dec si 1.39 + shr dl,1 1.40 + jnc @@nocat 1.41 + cmp cx,si 1.42 + jz @@nocat 1.43 + mov [word si],20h 1.44 + inc si 1.45 +@@nocat: 1.46 + sub bx,si 1.47 +@@cpylp: 1.48 + mov al,[bx+si] 1.49 + mov [si],al 1.50 + inc si 1.51 + cmp al,0 1.52 + jne @@cpylp 1.53 + mov ax,cx 1.54 + pop si 1.55 + ret 1.56 + 1.57 + endp _strcpy 1.58 + 1.59 + 1.60 +;*************************************************************** 1.61 +;char* strcat(const char* a,const char* b); 1.62 +;*************************************************************** 1.63 + global _strcat:near 1.64 + proc _strcat near 1.65 + 1.66 + mov dl,1 1.67 + jmp cat 1.68 + 1.69 + endp _strcat 1.70 + 1.71 + 1.72 +;*************************************************************** 1.73 +;char* strcatb(const char* a,const char* b); 1.74 +;*************************************************************** 1.75 + global _strcatb:near 1.76 + proc _strcatb near 1.77 + 1.78 + mov dl,3 1.79 + jmp cat 1.80 + 1.81 + endp _strcatb 1.82 + 1.83 + 1.84 +;*************************************************************** 1.85 +;void* malloc(unsigned sz); 1.86 +;*************************************************************** 1.87 + global _malloc:near 1.88 + proc _malloc near 1.89 + 1.90 + pop ax ;caller return address 1.91 + pop cx ; sz 1.92 + push cx 1.93 + push ax 1.94 + global malloc:near ; malloc(cx) 1.95 +malloc: 1.96 + mov ax,[_heap_top] 1.97 + mov bx,offset msg_malloc 1.98 + mov dx,-1400h ; MIN_STACK=_1k+PAGE_SIZE 1.99 + add dx,sp 1.100 + sub dx,ax ; can't overflow 1.101 + cmp dx,cx 1.102 + jb puts 1.103 + add [_heap_top],cx ; _BEG has zero'd heap 1.104 + ;mov bx,ax 1.105 +@@zalloc: 1.106 + ;mov [byte bx],0 1.107 + ;inc bx ; ZF=0 1.108 + ;loop @@zalloc 1.109 + ret 1.110 + 1.111 + endp _malloc 1.112 + 1.113 + 1.114 +;*************************************************************** 1.115 ;void puts(const char* s): 1.116 ;void putsz(const char* s): 1.117 ;*************************************************************** 1.118 @@ -64,12 +161,32 @@ 1.119 xchg ax,cx 1.120 mov bx,1 1.121 mov ah,40h 1.122 - jmp dos 1.123 + int 21h 1.124 + xor ax,ax ; ZF=1 (for malloc failure) 1.125 + ret 1.126 1.127 endp _puts 1.128 1.129 1.130 ;*************************************************************** 1.131 +;int fileattr(const char* name); 1.132 +;*************************************************************** 1.133 + global _fileattr:near 1.134 + proc _fileattr near 1.135 + 1.136 + pop ax ;caller return address 1.137 + pop dx ; name 1.138 + push dx 1.139 + push ax 1.140 + mov ax,4300h 1.141 + int 21h 1.142 + xchg ax,cx 1.143 + jmp chkc 1.144 + 1.145 + endp _fileattr 1.146 + 1.147 + 1.148 +;*************************************************************** 1.149 ;int open(const char* name, int flags); 1.150 ;*************************************************************** 1.151 global _open:near 1.152 @@ -87,15 +204,16 @@ 1.153 mov ah,3dh 1.154 dos: 1.155 int 21h 1.156 +chkc: 1.157 jnc doret 1.158 fail: 1.159 sbb ax,ax ; ax=-1 CF 1.160 cwd 1.161 doret: 1.162 ifndef NO386 1.163 - push dx 1.164 - push ax 1.165 - pop eax 1.166 + push dx ; see next_chunk:lseek 1.167 + push ax 1.168 + pop eax 1.169 endif 1.170 ret 1.171 1.172 @@ -170,7 +288,6 @@ 1.173 1.174 ;*************************************************************** 1.175 ;long lseek(int fd, long sz, int dir); 1.176 -;long rewind(int fd); 1.177 ;*************************************************************** 1.178 global _lseek:near 1.179 proc _lseek near 1.180 @@ -201,23 +318,6 @@ 1.181 endif 1.182 jmp dos 1.183 1.184 - global _rewind:near 1.185 -_rewind: 1.186 - pop ax ;caller return address 1.187 - pop bx ; fd 1.188 - push bx 1.189 - push ax 1.190 -rewind: 1.191 - ifndef NO386 1.192 - xor ecx,ecx 1.193 - xor dx,dx 1.194 - else 1.195 - xor ax,ax 1.196 - xor cx,cx 1.197 - cwd 1.198 - endif 1.199 - jmp lseek 1.200 - 1.201 endp _lseek 1.202 1.203 1.204 @@ -290,7 +390,7 @@ 1.205 global malloc_or_die:near ; malloc_or_die(cx) 1.206 malloc_or_die: 1.207 call malloc 1.208 - jz _diez 1.209 + jz _exit 1.210 ret 1.211 1.212 endp _malloc_or_die 1.213 @@ -298,7 +398,7 @@ 1.214 1.215 ;*************************************************************** 1.216 ;int die(const char* msg); 1.217 -;int diez(); 1.218 +;int exit(); 1.219 ;int abort(); 1.220 ;*************************************************************** 1.221 global _die:near 1.222 @@ -311,8 +411,8 @@ 1.223 global die:near ; die(bx) 1.224 die: 1.225 call puts 1.226 - global _diez:near 1.227 -_diez: 1.228 + global _exit:near 1.229 +_exit: 1.230 mov al,[_no_exit] 1.231 cmp al,0 1.232 jne @@hang 1.233 @@ -333,6 +433,87 @@ 1.234 1.235 1.236 ;*************************************************************** 1.237 +;u32* malloc_bufv_or_die(struct image_himem *m); 1.238 +;*************************************************************** 1.239 + global _malloc_bufv_or_die:near 1.240 + proc _malloc_bufv_or_die near 1.241 + 1.242 + pop bx ;caller return address 1.243 + pop ax 1.244 + push ax 1.245 + push bx 1.246 + push si 1.247 + xchg ax,si 1.248 + mov ecx,[si+6] ; m->size 1.249 + dec ecx 1.250 + shr ecx,12 1.251 + inc cx ; cnt = (m->size+PAGE_MASK)/PAGE_SIZE; 1.252 + push cx 1.253 + inc cx ; cnt+1 1.254 + shl cx,2 ; bufv => vcpi => vm86 1.255 +; our malloc zeroes allocated mem: bufv[cnt]=0; 1.256 +; Allocate pages, storing addrs in addrbuf 1.257 + call malloc_or_die 1.258 + pop cx 1.259 + push cx ; _sort:nel 1.260 + push ax ; _sort:base 1.261 + mov [si+18],ax ; m->bufv 1.262 + xchg ax,bx 1.263 +@@vcpi_alloc: 1.264 + xor edx,edx 1.265 + mov ax,0DE04h 1.266 + int 67h 1.267 + or ah,ah 1.268 + jz @@ok 1.269 + mov bx,offset vcpi_alloc_err 1.270 + jmp die 1.271 +@@ok: 1.272 + mov [bx],edx 1.273 + add bx,4 1.274 + loop @@vcpi_alloc 1.275 +@@again: 1.276 + call _sort 1.277 + extrn _initrd 1.278 + cmp si,offset _initrd 1.279 + jne @@quit 1.280 + pop ax 1.281 + pop cx 1.282 + push cx ; _sort:nel 1.283 + push ax ; _sort:base = m->bufv 1.284 +;again: 1.285 +; for (i = cnt-1; i >= 0; i--) { 1.286 +@@chkloop: 1.287 + mov bx,cx 1.288 + dec bx 1.289 +; if (m->bufv[i] > m->fallback+i*_4k && m->bufv[i] < m->fallback+m->size) { 1.290 + shl bx,2 1.291 + add bx,ax ; m->bufv 1.292 + mov edx,[bx] ; m->bufv[i] 1.293 + sub edx,[si+2] ; m->fallback 1.294 + cmp edx,[si+6] ; m->size 1.295 + jae @@chknext 1.296 + shr edx,12 1.297 + cmp dx,cx 1.298 + jb @@chknext 1.299 +; m->bufv[i] = vcpi_alloc_or_die(); 1.300 +; sort(m->bufv,cnt); 1.301 +; goto again; 1.302 + mov cx,1 1.303 + jmp @@vcpi_alloc 1.304 +; } 1.305 +; } 1.306 +@@chknext: 1.307 + loop @@chkloop 1.308 +@@quit: 1.309 + pop ax 1.310 + pop cx 1.311 + pop si 1.312 + ret 1.313 + 1.314 + endp _malloc_bufv_or_die 1.315 + 1.316 + 1.317 +;*************************************************************** 1.318 ;void next_chunk(struct image_himem *m); 1.319 ;*************************************************************** 1.320 proc _next_chunk near 1.321 @@ -341,7 +522,7 @@ 1.322 pop ax 1.323 push ax 1.324 push bx 1.325 - push di 1.326 + push si di 1.327 xchg ax,di 1.328 mov bx,[di] ; m->fd 1.329 call close 1.330 @@ -355,7 +536,6 @@ 1.331 mov bx,[di+28] ; m->state 1.332 cmp al,[bx] ; "" 1.333 jz @@end 1.334 - push si 1.335 mov si,bx 1.336 @@scan: 1.337 lodsb 1.338 @@ -373,9 +553,9 @@ 1.339 xchg ax,dx ; O_RDONLY 1.340 call open 1.341 pop [word si] ; restore string 1.342 - pop si 1.343 jc @@die 1.344 mov [di],ax ; m->fd 1.345 + mov [di+30],ax ; m->fd2close 1.346 mov dx,2 ; SEEK_END 1.347 xchg ax,bx 1.348 ifndef NO386 1.349 @@ -391,21 +571,26 @@ 1.350 mov bx,[di] ; m->fd 1.351 ifndef NO386 1.352 push eax 1.353 - call rewind 1.354 + xor ecx,ecx 1.355 + xor dx,dx 1.356 + call lseek ; rewind 1.357 pop eax 1.358 @@end: 1.359 mov [di+22],eax ; m->chunk_size 1.360 else 1.361 push ax 1.362 push dx 1.363 - call rewind 1.364 + xor ax,ax 1.365 + xor cx,cx 1.366 + cwd 1.367 + call lseek ; rewind 1.368 pop dx 1.369 pop ax 1.370 @@end: 1.371 mov [di+22],ax ; m->chunk_size 1.372 mov [di+24],dx 1.373 endif 1.374 - pop di 1.375 + pop di si 1.376 ret 1.377 1.378 endp _next_chunk 1.379 @@ -424,6 +609,7 @@ 1.380 ;22 u32 chunk_size; 1.381 ;26 void (*next_chunk)(struct image_himem *); 1.382 ;28 u16 state; 1.383 +;30 u16 fd2close; 1.384 ;}; 1.385 ;*************************************************************** 1.386 global _open_image:near 1.387 @@ -470,7 +656,7 @@ 1.388 and al,0FCh 1.389 add [di+6],ax ; m->size += m->chunk_size 1.390 adc [di+8],dx 1.391 - inc cx 1.392 + inc cx ; jcxnz 1.393 loop @@next 1.394 endif 1.395 mov [di+28],si ; m->state 1.396 @@ -508,49 +694,76 @@ 1.397 endif 1.398 mov di,[m] 1.399 @@loop: 1.400 - mov ax,[word sz] 1.401 - mov cx,[di+22] ; m->chunk_size 1.402 - cmp ax,cx 1.403 + ifndef NO386 1.404 + xor ecx,ecx 1.405 + mov cx,[word sz] 1.406 +@@chksz: 1.407 + mov eax,[di+22] ; m->chunk_size 1.408 + cmp ecx,eax 1.409 + jb @@szok 1.410 + xchg eax,ecx 1.411 + else 1.412 + mov cx,[word sz] 1.413 +@@chksz: 1.414 + mov ax,[di+22] ; lo m->chunk_size 1.415 + cmp cx,ax 1.416 jb @@szok 1.417 cmp [word di+24],0 ; hi m->chunk_size 1.418 jne @@szok 1.419 xchg ax,cx 1.420 + endif 1.421 @@szok: 1.422 - push ax 1.423 + jcxz image_done 1.424 + push cx 1.425 push [word data] 1.426 push [word di] 1.427 call _read 1.428 - pop cx 1.429 + pop dx 1.430 pop bx 1.431 + pop dx 1.432 + jc image_done 1.433 add bx,ax 1.434 - pop cx 1.435 xor cx,cx 1.436 + ifndef NO386 1.437 + cwde ; ax < 8000h 1.438 + cwd 1.439 + sub [di+22],eax 1.440 + else 1.441 + cwd ; ax < 8000h 1.442 sub [di+22],ax 1.443 - sbb [di+24],cx 1.444 + sbb [di+24],dx 1.445 + jnz @@fill 1.446 + cmp [di+22],dx 1.447 + endif 1.448 + jnz @@fill 1.449 + dec cx 1.450 @@fill: 1.451 test al,3 1.452 je @@filled 1.453 - mov [bx],cl 1.454 + mov [bx],dl 1.455 inc bx 1.456 inc ax 1.457 jmp @@fill 1.458 @@filled: 1.459 + ifndef NO386 1.460 + sub [di+10],eax ; m->remaining 1.461 + else 1.462 + sub [di+10],ax ; m->remaining 1.463 + sbb [di+12],dx 1.464 + endif 1.465 add [bp-4-2],ax 1.466 add [word data],ax 1.467 sub [word sz],ax 1.468 - jz image_done 1.469 - mov cx,[di+22] ; lo m->chunk_size 1.470 - or cx,[di+24] ; hi m->chunk_size 1.471 - jnz image_done 1.472 - or cx,[di+26] ; m->next_chunk 1.473 - jz image_done 1.474 + pushf 1.475 + and cx,[di+26] ; m->next_chunk 1.476 + jz @@same_chunk 1.477 push di 1.478 call cx ; m->next_chunk() 1.479 pop di 1.480 - mov cx,[di+22] ; lo m->chunk_size 1.481 - or cx,[di+24] ; hi m->chunk_size 1.482 - jz image_done 1.483 - jmp @@loop 1.484 +@@same_chunk: 1.485 + popf 1.486 + jnz @@loop 1.487 + jmp image_done 1.488 1.489 endp _read_image 1.490 1.491 @@ -561,20 +774,35 @@ 1.492 global _strtol:near 1.493 proc _strtol near 1.494 1.495 -;TODO NO386 1.496 ifndef NO386 1.497 pop ax ;caller return address 1.498 pop cx ; s 1.499 push cx 1.500 push ax 1.501 xor ebx,ebx 1.502 - jcxz @@end 1.503 + jcxz @@jncend 1.504 push si 1.505 mov si,cx 1.506 xor ecx,ecx 1.507 xor eax,eax 1.508 - mov cl,10 ; radix 1.509 lodsb 1.510 + mov dl,20h 1.511 + or dl,al 1.512 + cmp dl,'n' ; vga=normal 1.513 + je @@vga 1.514 + dec cx 1.515 + cmp dl,'e' ; vga=extended 1.516 + je @@vga 1.517 + dec cx 1.518 + cmp dl,'a' ; vga=ask 1.519 + jne @@notvga 1.520 +@@vga: 1.521 + dec cx 1.522 + xchg ax,cx 1.523 + cwd 1.524 + jmp popsiret 1.525 +@@notvga: 1.526 + mov cx,10 ; radix 1.527 cmp al,'+' 1.528 je @@radixskip 1.529 cmp al,'-' 1.530 @@ -627,6 +855,7 @@ 1.531 shl ebx,cl 1.532 @@noshift: 1.533 popf 1.534 +@@jncend: 1.535 jnc @@end 1.536 neg ebx 1.537 @@end: 1.538 @@ -645,10 +874,28 @@ 1.539 xor ax,ax 1.540 cwd 1.541 xchg ax,di 1.542 - jcxz @@end 1.543 + jcxz @@goend 1.544 mov si,cx 1.545 + lodsb 1.546 + mov dl,20h 1.547 + or dl,al 1.548 + mov cx,-1 1.549 + cmp dl,'n' ; vga=normal 1.550 + je @@vga 1.551 + dec cx 1.552 + cmp dl,'e' ; vga=extended 1.553 + je @@vga 1.554 + dec cx 1.555 + cmp dl,'a' ; vga=ask 1.556 + jne @@notvga 1.557 +@@vga: 1.558 + xchg ax,cx 1.559 + cwd 1.560 + jmp popsiret 1.561 +@@goend: 1.562 + jmp @@end 1.563 +@@notvga: 1.564 mov cx,10 ; radix 1.565 - lodsb 1.566 cmp al,'+' 1.567 je @@radixskip 1.568 cmp al,'-' 1.569 @@ -752,13 +999,12 @@ 1.570 ifndef fastsort 1.571 ; bubble sort 1.572 push si 1.573 - cmp cx,2 1.574 - jl popsiret 1.575 shl cx,2 1.576 @@loop: 1.577 xor ax,ax 1.578 - jcxz popsiret 1.579 mov si,4 1.580 + cmp cx,si 1.581 + jbe popsiret 1.582 @@next: 1.583 mov edx,[bx+si-4] 1.584 cmp edx,[bx+si] 1.585 @@ -859,41 +1105,6 @@ 1.586 endp _sort 1.587 1.588 1.589 -;*************************************************************** 1.590 -;void* malloc(unsigned sz); 1.591 -;*************************************************************** 1.592 - global _malloc:near 1.593 - proc _malloc near 1.594 - 1.595 - pop ax ;caller return address 1.596 - pop cx ; sz 1.597 - push cx 1.598 - push ax 1.599 - global malloc:near ; malloc(cx) 1.600 -malloc: 1.601 - mov ax,[_heap_top] 1.602 - mov bx,sp 1.603 - sub bh,14h ; MIN_STACK=_1k+PAGE_SIZE 1.604 - sub bx,cx 1.605 - jb @@outofmem 1.606 - cmp bx,ax 1.607 - jb @@outofmem 1.608 - add [_heap_top],cx ; _BEG has zero'd heap 1.609 - ;mov bx,ax 1.610 -@@zalloc: 1.611 - ;mov [byte bx],0 1.612 - ;inc bx ; ZF=0 1.613 - ;loop @@zalloc 1.614 - ret 1.615 -@@outofmem: 1.616 - mov bx,offset msg_malloc 1.617 - call puts 1.618 - xor ax,ax ; ZF=1 1.619 - ret 1.620 - 1.621 - endp _malloc 1.622 - 1.623 - 1.624 ifdef NO386 1.625 ;*************************************************************** 1.626 ;u16 topseg(); 1.627 @@ -905,7 +1116,7 @@ 1.628 jnc @@max640k 1.629 mov ax,640 ; 9000 1.630 @@max640k: 1.631 - sub al,040h 1.632 + sub ax,028h 1.633 and al,0C0h 1.634 mov cl,6 1.635 shl ax,cl 1.636 @@ -914,6 +1125,41 @@ 1.637 endp _topseg 1.638 endif 1.639 1.640 +;*************************************************************** 1.641 +;void rmcpy(void* rmbuf, u16 rmsize); 1.642 +;*************************************************************** 1.643 + global _rmcpy:near 1.644 + proc _rmcpy near 1.645 + 1.646 + pop bx ;caller return address 1.647 + pop ax ; rmbuf 1.648 + pop cx ; rmsize 1.649 + push cx 1.650 + push ax 1.651 + push bx 1.652 + push si di es 1.653 + xchg ax,si 1.654 + xor di,di 1.655 + ifdef NO386 1.656 + call _topseg 1.657 + mov es,ax 1.658 + else 1.659 + push 9000h 1.660 + pop es 1.661 + endif 1.662 + cld 1.663 + rep 1.664 + movsb 1.665 + extrn _cmdline:word 1.666 + mov si,[_cmdline] 1.667 + mov di,8000h 1.668 + mov ch,10h ; 4k 1.669 + rep 1.670 + movsb 1.671 + pop es di si 1.672 + ret 1.673 + 1.674 + endp _rmcpy 1.675 1.676 ends _TEXT 1.677