flat assembler
Message board for the users of flat assembler.

Index > DOS > sprintf for dos via fasmg, i need help to compile: mov ax,[b

Author
Thread Post new topic Reply to topic
uor99



Joined: 04 Dec 2014
Posts: 42
uor99 04 Oct 2016, 23:36
Code:
;sprintf for dos via fasmg, i need help to compile: mov ax,[bp+dx]
;(https://board.flatassembler.net/topic.php?p=190878)
;
include '8086.inc'
include 'format/mz.inc'
macro stdcall proc,args&
        iterate arg, args
                indx 1+%%-%
                mov bx,arg
                push bx
        end iterate
        call proc
end macro
entry code:start        ; program entry point
stack 100h      ; stack size
        segment code
start:
        push cs
        pop ds
        call main
        mov ah,4ch
        int 21h
; sprintf(soutput, sformat, [args])
; sformat: string containing the pattern of the output string
;%s: string
;%d: decimal number
;%h: hexadecimal number in lower case
;%h: hexadecimal number in upper case
;
;%d, %h and %h also accepts ":[number of fixed digits]"
;the number in the square brackets should then be in[0..9] and equal one less the number of fixed digits...
; soutput: pointer to buffer which will receive the output string
; [args]: ...
;
; alpha_addition db 0
; digit_count db 0
; put these two variables in the data section:
max_dx equ 8
sprintf:
        ;enter 0,0
        push bp
        mov bp,sp
        mov dx,max_dx
        mov di,[bp+4]
        mov si,[bp+6]
        
        push dx
        mov dx,si
        ;call _tips
        pop dx
 .loop:
        lodsb
        cmp al,"%"
        je .format
        stosb
        test al,al
        jnz .loop
 .done:
        xor ax,ax
        jmp .finish
 .error:
        or ax,-1
 .finish:
        ;leave
        mov sp, bp
        pop bp
        
        pop ax
        sub dx,max_dx/2;8
        add sp,dx
        jmp ax
 .format:
        lodsb
        cmp al,"%"
        jne .not_percent
        stosb
        jmp .loop
 .not_percent:
        mov [digit_count],0
        push ax
        cmp byte [si],":"
        jne .not_extra
        lodsb
        cmp byte [si],":"
        je .not_extra
        lodsb
        cmp al,"0"
        jl .error
        cmp al,"9"
        jg .error
        sub al,2fh
        mov [digit_count],al
 .not_extra:
        pop ax
        cmp al,"s"
        je .string
        cmp al,"h"
        je .number.lhex
        cmp al,"h"
        je .number.uhex
        cmp al,"d"
        jne .error
 .number.dec:
        mov bx,10
        call .number
        jmp .loop
 .number.lhex:
        mov byte [alpha_addition],27h
        mov bx,16
        call .number
        jmp .loop
 .number.uhex:
        mov byte [alpha_addition],7
        mov bx,16
        call .number
        jmp .loop
 .number:
        push bx
        mov bx,bp
        add bx,dx
        mov ax,[bp+dx];[bp+dx]
        pop bx
        add dx,max_dx/4;4
        push dx
        xor cx,cx
 .number.loop:
        xor dx,dx
        div bx
        push dx
        inc cx
        test ax,ax
        jnz .number.loop
        cmp [digit_count],0
        je .number.putchar
        push cx
        mov ax,cx
        mov cl,byte [digit_count];;;
        sub cx,ax
        jz .number.prefix.end
        js .number.prefix.end
        mov al,"0"
 .number.prefix:
        stosb
        loop .number.prefix
 .number.prefix.end:
        pop cx
 .number.putchar:
        pop ax
        add al,30h
        cmp al,39h
        jle .number.putchar.nonalpha
        add al,[alpha_addition]
 .number.putchar.nonalpha:
        stosb
        loop .number.putchar
        pop dx
        ret
 .string:
        push si
        push bx
        mov bx,bp
        add bx,dx
        mov si,[bp+dx];[bp+dx]
        pop bx
        add dx,max_dx/4;4
 .string.loop:
        lodsb
        test al,al
        jz .string.end
        stosb
        jmp .string.loop
 .string.end:
        pop si
        jmp .loop
main:
        push bp
        mov bp, sp
        ;
        stdcall sprintf,soutput,sformat,hi,16,start2,start
        add sp,12
        ;
        mov dx,soutput
        mov ah,9
        int 21h
        ;
        mov dx,2
        add dx,30h
        mov ah,2
        int 21h
        mov sp, bp
        pop bp
        ret

;segment data
        alpha_addition db 0
        digit_count db 0
        sformat db "%s! %d-lol",13,10
                db "%s = offset: 0x%h:7$"
        hi      db "hi$"
        start2  db "start$"
        soutput rb 100h
    
Post 04 Oct 2016, 23:36
View user's profile Send private message ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20401
Location: In your JS exploiting you and your system
revolution 05 Oct 2016, 09:23
uor99 wrote:
i need help to compile: mov ax,[bp+dx]
DX cannot be used as an addressing register. Only SI, DI, BP and BX.
Post 05 Oct 2016, 09:23
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 05 Oct 2016, 09:39
Also, even not all combination of these four are possible. Each of them can be used individually and there are just four combinations of two that are allowed: BX+SI, BX+DI, BP+SI and BP+DI.
Post 05 Oct 2016, 09:39
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.