flat assembler
Message board for the users of flat assembler.

Index > Main > 16 bit int to str and str to int routines

Author
Thread Post new topic Reply to topic
geekbasic@gmx.com



Joined: 25 Oct 2022
Posts: 71
Location: Arizona
geekbasic@gmx.com 30 Nov 2022, 03:08
After much looking, I am unable to find any 16 bit examples of inttostr and strtoint routines.

So far, I have almost every string operation working except these.

I'm not sure why I can't get any examples to work, but I am using dw and db for the data values.

dw for the value
db for the string (0 terminated)
Post 30 Nov 2022, 03:08
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 819
Location: Russia
macomics 30 Nov 2022, 04:22
dw = 16-bit integer -> db 17 dup (0)

Code:
inttostr: ; ax = integer, cx = base, out ds:si = buffer
    push si
    push bp
    mov bp, sp
    test ax, ax
    jns @f
    mov byte [si], '-'
    inc si
    neg ax

  @@:
    xor dx, dx
    div cx
    push dx
    test ax, ax
    jnz @b

 @@:
    pop ax
    cmp al, 10
    jc .num
    add al, 'A' - '0' - 10

 .num:
    add al, '0'
    mov [si], al
    inc si
    cmp sp, bp
    jc @b
    mov byte [si], 0
    mov ax, si
    leave
    pop si
    sub ax, si
    retn ; retf (ax = length)    


Code:
strtoint: ; ds:si = string, cx = base
    push bx
    push si
    xor ax, ax
    xor bx, bx
    cmp byte [si], '-'
    jnz .beg
    inc si
    jmp .beg

 @@:
    sub bl, '0'
    cmp bl, 10
    jc .num
    sub bl, 'A' - '0' + 10
    cmp bx, cx
    jae @f

 .num:
    mul cx
    inc si
    add ax, bx

 .beg:
    mov bl, [si]
    test bl, bl
    jnz @b

 @@:
    pop si
    pop bx
    cmp byte [si], '-'
    jnz @f
    neg ax

 @@:
    retn ; retf (ax = integer)    
Post 30 Nov 2022, 04:22
View user's profile Send private message Reply with quote
geekbasic@gmx.com



Joined: 25 Oct 2022
Posts: 71
Location: Arizona
geekbasic@gmx.com 30 Nov 2022, 19:07
I am barely starting to grasp the pointer instructions.
For the ds:si part of the call, do i use LEA SI,[text]?

For the cx, what is meant by base? Does this mean I can specify binary, decimal, etc? Or does it mean something else.

Reading the manual, I see that "ret is the equivalent for retn." Does this mean that I can use either ret or retn? Is there a difference?

I was wondering that the @@, @f and, @b meant. Just looked it up and that's really cool! Anonymous labels. I will start using this feature in my code wherever I can.

Thank you!
Post 30 Nov 2022, 19:07
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 819
Location: Russia
macomics 30 Nov 2022, 20:01
geekbasic@gmx.com wrote:
For the ds:si part of the call, do i use LEA SI,[text]?
If text is in the segment that ds is configured for.

or
Code:
lds si, [text_ptr]

...
label text_ptr dword
; Convert the linear address of text to a segment and offset for 16-bit addressing
.off dw .str and 0xFFFF
.seg dw (.str and 0xF0000) shr 4
.str db '1234', 0    

In 16-bit mode, the following addressing mode operates: each memory cell has a 20-bit linear address, which is divided into 2 components. The first component contains the address of the paragraph (16-byte value, located in the segment register), the second (located in the base or index register or specified directly) - the address of the byte relative to the 64 kb block starting with the paragraph specified in the segment register.
A 20-bit address allows access to 1 MB of memory, but such an addressing mechanism creates a collision, due to which about 65520 bytes more are available for addressing (after opening the A20 gate).

geekbasic@gmx.com wrote:
For the cx, what is meant by base? Does this mean I can specify binary, decimal, etc?
Yes.

geekbasic@gmx.com wrote:
Reading the manual, I see that "ret is the equivalent for retn." Does this mean that I can use either ret or retn? Is there a difference?
ret/retn is a near return instruction. It reloads only the return address (ip) from the stack.

retf is a far return instruction. It reloads from the stack not only the return address (ip), but also the segment address (cs). Only 4 bytes. In order to write retf at the end, it is necessary that the function call be appropriate. For example:
Code:
call text:inttostr ; far call of the `inttostr` function in the `text` segment    


There is also an iret - return from an interrupt. It reloads from the stack not only the return address (ip) and segment address (cs), but also the value of the flag register (fl). Only 6 bytes. She also performs additional manipulations with flags. This instruction is mainly used in interrupt vector handlers, but it can also be used when switching tasks in protected mode and changing the privilege level.

geekbasic@gmx.com wrote:
I was wondering that the @@, @f and, @b meant. Just looked it up and that's really cool! Anonymous labels. I will start using this feature in my code wherever I can.
https://flatassembler.net/docs.php?article=manual#1.2.3
Post 30 Nov 2022, 20:01
View user's profile Send private message Reply with quote
geekbasic@gmx.com



Joined: 25 Oct 2022
Posts: 71
Location: Arizona
geekbasic@gmx.com 30 Nov 2022, 22:45
Your explanation is helpful. I am now able to make the code work.

Thank you!
Post 30 Nov 2022, 22:45
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-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.