flat assembler
Message board for the users of flat assembler.

Index > Windows > Please help, lost, confused... would appreciate it.

Author
Thread Post new topic Reply to topic
ughisuck



Joined: 31 Mar 2021
Posts: 1
ughisuck
Hi, I'm trying to make a calculator to learn and I'm running into an issue when I use ReadConsole. It's loading the value but then I want to chop off the extra string characters so I can convert the string to a decimal representation but I'm struggling just at the string part and chopping off the extra two bytes "\r\n"

Here's the code, int3 is already in the area I was trying (I put comments on what I'm thinking as well).

Essentially want to take the two inputs:

1. 100 which turns into "100\r\n"
2. 50 which turns into "50\r\n"
3. Chop off \r\n on both
4. Convert 100 and 50 to decimal
5. Add them

Would love tips on at least getting to 3, if any tips on 4 I'd really appreciate it so much. Thank you.


Code:
format PE64 Console
include 'win64axp.inc'


.code
start:
        sub     rsp,8

        invoke  GetStdHandle,STD_INPUT_HANDLE
        mov     [stdin],eax
        invoke  GetStdHandle,STD_OUTPUT_HANDLE
        mov     [stdout],eax


        ; outer loop - command
        invoke  WriteConsole,[stdout],addr sError,sErrorSz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0
        mov     edi,buff
        ; ? cmds
        cmp     byte [edi],'+'
        je      HANDLEADD
        cmp     byte [edi],'-'
        je      HANDLESUB
        cmp     byte [edi],'*'
        je      HANDLEMUL
        cmp     byte [edi],'/'
        je      HANDLEDIV
        jmp     error



        jmp     error

error:
        mov rcx,sError
        invoke WriteConsole,[stdout],addr sError,sErrorSz, addr count, 0

        jmp clear
HANDLEADD:
        push    r12
        push    r13


        invoke  WriteConsole,[stdout],addr sAdd,sAddSz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0
;:: If I type in "100" the buffer will have "100\r\n"
;:: I want to take the bytes written to -2 and put it anywhere (stack or register is fine)
;:: So basically just push 100. In my mind this makes sense: push count-2 [buff-count-2] (so it would only push the byte size of count - 2 bytes)
;:: but all I'm getting is something like the address and not sure how to take off the two bytes, maybe I could do string operation but not sure?
;:: push count-2
;::
;::  0000000000402000  31 30 30 0D 0A 00 00 00 00 00 00 00 00 00 00 00  100.............
                                                                                                
        mov eax,buff

        int3
        push buff
    ;    mov by
        push qword [eax]
        push count-2
        int3
        movzx   r12,byte [buff]

        ; ask for second number
        invoke  WriteConsole,[stdout],addr sAdd2,sAdd2Sz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0
        int3
        movzx   r13,byte [buff]

        ; add it!
        add     r12,r13


        pop     r13
        pop     r12
        jmp     clear
HANDLESUB:
        invoke WriteConsole,[stdout],addr sAdd,sErrorSz, addr count, 0

        jmp     clear
HANDLEMUL:
        invoke WriteConsole,[stdout],addr sAdd,sErrorSz, addr count, 0

        jmp     clear
HANDLEDIV:
        invoke WriteConsole,[stdout],addr sAdd,sErrorSz, addr count, 0

        jmp     clear
equal:
        invoke WriteConsole,[stdout],addr sAdd,sErrorSz, addr count, 0

        jmp     clear
notequal:
        invoke WriteConsole,[stdout],addr sAdd,sErrorSz, addr count, 0

        jmp     clear
clear:
        xor rcx,rcx
        xor rdi,rdi
        xor rsi,rsi
        xor eax,eax
        xor ecx,ecx
        jmp start
.data
        buff    rb 1024
        buff2   rb 1024
        count   dd ?
        outSz   dd ?
        count2  dd ?
        stdin   dd ?
        stdout  dd ?
        pMath   dd 0

        sError          db 'Try +, -, *, /',10,0
        sErrorSz        = $ - sError
        sEqual          db 'It is equal',10,0
        sEqualSz        = $ - sEqual
        sNotEqual       db 'Not Equal',10,0
        sNotEqualSz     = $ - sNotEqual
        sAdd            db 'First number to add?',10,0
        sAddSz          = $ - sAdd
        sAdd2           db 'Second number?',10,0
        sAdd2Sz         = $ - sAdd2
        sMul            db 'First number to multiply?',10,0
        sMulSz          = $ - sMul
        sMul2           db 'Second number to multiply?',10,0
        sMul2Sz         = $ - sMul2
        sSub            db 'First number to subtract?',10,0
        sSubSz         = $ - sSub
        sSub2           db 'Second number to subtract?',10,0
        sSub2Sz         = $ - sSub2
        sDiv            db 'First number to divide?',10,0
        sDivSz          = $ - sDiv
        sDiv2           db 'Second number to divide?',10,0
        sDiv2Sz         = $ - sDiv2

.end start
    
Post 31 Mar 2021, 11:18
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3387
Location: vpcmipstrm
bitRAKE
There are many ways to move forward. Beginning at the start of the buffer, convert digits and stop when a non-digit is reached. Another way is to scan from the end of the string with the [count] returned.

Presently you are getting the address in EAX of the buffer. A number would be in the range '0' to '9'.
Code:
cmp byte [rax],'0'
jc .too_low
cmp byte [rax],'9'+1
jnc .too_high

; now there is a digit at byte [rax]    
That should be sufficient for you to complete all tasks. What debugger are you using?

_________________
¯\(°_o)/¯ unlicense.org
Post 31 Mar 2021, 14:01
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 213
Location: Malaysia
FlierMate
ughisuck wrote:

Essentially want to take the two inputs:

1. 100 which turns into "100\r\n"
2. 50 which turns into "50\r\n"
3. Chop off \r\n on both
4. Convert 100 and 50 to decimal
5. Add them

(code snipped)

Would love tips on at least getting to 3, if any tips on 4 I'd really appreciate it so much. Thank you.


Hi, I am learning too. I have modify your code a little bit so that now it can add any two single-digit numbers!

Code:
HANDLEADD:
        push    r12
        push    r13


        invoke  WriteConsole,[stdout],addr sAdd,sAddSz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0
    ;    mov eax,buff
     ;   push buff
    ;    mov by
    ;    push qword [eax]
    ;    push count-2
        movzx   r12, byte [buff]
        sub r12, 48

        ; ask for second number
        invoke  WriteConsole,[stdout],addr sAdd2,sAdd2Sz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0
        movzx   r13,byte [buff]
        sub r13, 48

        ; add it!
        add     r12,r13

invoke wsprintf,result,'%d',r12
invoke  WriteConsole,[stdout],result,4,addr outSz,0


        pop     r13
        pop     r12
        jmp     clear
HANDLESUB:        
......
......
.data
        buff    rb 1024
        buff2   rb 1024
        result  rb 64        


Just to serve as an not-so-good example.


Description: Example output when adding two single-digit numerical values
Filesize: 49.52 KB
Viewed: 1577 Time(s)

calc.JPG


Post 02 Apr 2021, 12:34
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 213
Location: Malaysia
FlierMate
This is my new code, with reference to other sources on how to convert string to integer.

Code:
HANDLEADD:
        push    r12
        push    r13


        invoke  WriteConsole,[stdout],addr sAdd,sAddSz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0
    ;    mov eax,buff
     ;   push buff
    ;    mov by
    ;    push qword [eax]
    ;    push count-2

        call convert
        mov r12d, eax
        ;movzx   r12, byte [buff]
        ;sub r12, 48

        ; ask for second number
        invoke  WriteConsole,[stdout],addr sAdd2,sAdd2Sz,addr outSz,0
        invoke  ReadConsole,[stdin],buff,1024,addr count,0

        ;movzx   r13,byte [buff]
        ;sub r13, 48
        call convert
        mov r13d,eax

        ; add it!
        add     r12d,r13d

invoke wsprintf,result,'%d',r12d
invoke  WriteConsole,[stdout],result,6,addr outSz,0


        pop     r13
        pop     r12
        jmp     clear

convert:
    xor eax,eax
    lea ebx, [buff]
.redo:
    movzx ecx, byte [ebx]
    inc ebx
    cmp ecx, '0'
    jb .done
    cmp ecx, '9'
    ja .done
    sub ecx, '0'
    imul eax, 10
    add eax,ecx
    jmp .redo
.done:
    ret   
    


Description: The add numbers function is now working
Filesize: 54.27 KB
Viewed: 1560 Time(s)

calc2.JPG


Post 02 Apr 2021, 14:22
View user's profile Send private message 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.