0

So I have a macro code from help of the teacher (this worked previously)

    %macro aBin2int 2

    push rbp
    mov rbp, rsp
    sub rsp, 8
    push rcx
    push rdx

    mov dword [%2], 0
    mov dword [rbp - 4], 0    
    mov dword [rbp - 8], 2    
    mov rax, 0
    mov rdx, 0
    mov rcx, 0
    mov r10, 0

    %%nxtCR:
        movzx eax, byte [%1 + r10]
       cmp eax, NULL
        je %%controlDone
        cmp eax, "0"
        jae %%adding
    %%adding:
        sub eax, "0"
        mov dword [rbp - 4], eax
        mov eax, dword [%2]
        mul dword [rbp - 8]
        add eax, dword [rbp - 4]
        mov dword [%2], eax
        inc r10
        jmp %%nxtCR
    %%completed:

    pop rdx
    pop rcx

    mov rsp, rbp
    pop rbp

    %endmacro

Now I have to call a function that calls another function.

; ******************************************************************
;  Function getIterations()
;   Performs error checking, converts ASCII/binary to integer.
;   Command line format (fixed order):
;     "-it <binaryNumber> -rs <binaryNumber>"

; -----
;  Arguments:
;   1) ARGC, double-word, value
;   2) ARGV, double-word, address
;   3) iterations count, double-word, address
;   4) rotate spped, double-word, address

So this getIteration calls

; *******************************************************************************
;  Simple function to convert ternary string to integer.
;   Reads string and converts to intger

; -----
;  HLL Call
;   bool = ternary2int(&str, &int);

; -----
;  Arguments passed:
;   1) string, address, rdi
;   2) integer, address, rsi

; -----
;  Returns
;   1) integer value (via passed address)
;   2) bool, TRUE if valid conversion, FALSE for error

aBin2int function (this read binary strings into a decial digit)

This is working okay as macro, but I am troubling with changing it into a function.

            mov rbx, qword[rsi + 16]
                mov r10, 0
    
        binaryCheck:
            mov rax, 0

            mov al, byte[rbx + r10]
            cmp al, NULL
            je convertBinary
            cmp al, "2"
            jae itsNoBinary

            cmp al, "0"
            jb itsNoBinary
            inc r10
            jmp binaryCheck

        convertBinary:
            mov r15, 0
            mov rdi, rbx
            mov rsi, r15
        call aBin2int

Could anyone please help me with this?

ecm
  • 2,583
  • 4
  • 21
  • 29
Jawj
  • 1
  • 2
    You posted a lot of non-relevant information. Where is your attempt to convert the macro into a function? You have to pick up a calling conventing for the two arguments, say `rdi` for the binary numeral and `rdi` for the pointer to the number. The macro seems to be using a mistyped label (`controlDone` vs `complete`?) and is generally of very very poor quality. It would be easier to return the result in `rax` instead of saving it in a pointer. BTW you can't simply call a macro like it's a function. – Margaret Bloom Jun 23 '22 at 07:36
  • 1
    This looks very inefficient. Why would you want to store the base (`2`) into memory, instead of using a register? Or much better, an immediate `imul eax, eax, 2` since you're not using the high-half EDX result of `mul`. Or even better, `shl eax, 1`. Did you originally adapt this from compiler output where you forgot to enable optimization? All those store/reloads inside the loop make your code over-complicated. – Peter Cordes Jun 23 '22 at 12:46
  • [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) shows an efficient string->int function for base 10. Replacing `total = total*10 + digit` with `total = total*2 + digit` is all you need to change, just one LEA instead of two. Or `shl` / `or`. – Peter Cordes Jun 23 '22 at 12:49

0 Answers0