So I have a practice exam for assemblyx86 language
Write a void function, encryptStr(), that will encrypt a string. To encrypted the string, simply add one to each alphabetic (upper and lower case letters) character. All non-alphabetic characters (numbers, punctuation, spaces, etc.) are unchanged. Thus
Hello Zoo 12becomesIfmmp [pp 12. The string is NULL terminated.The function should return
- total count of all letters
- the count of ASCII digits ("0"-"9") and
- the total string length. The function is called as follows:
encryptStr(inStr, <rCnt, &digitCnt, &strLen);Where inStr is the input string ltrCnt, digitCnt, and strLen are the addresses of the unsigned double-word variables of where to return the appropriate values (note, passed variables not initialized to zero).
Your function must use the standard calling convention to access the arguments and save/restore only the appropriate registers used. You must define and show any additional data declarations that are used (if any). Points will be deducted for especially poor or inefficient solutions.
I tried to do it myself and wondering whether this is make sense....
       section  .data
        EXIT_SUCCESS        equ 0
        SYS_exit        equ 60
        NULL        equ     0
        SYS_write   equ 1
        STDOUT      equ 1
        input           db "11110", 0
    section .bss
        IntCnt      resd 1
        digitCnt    resd 1
        strLen      resd 1
    section .text
    global  _start
    _start:
        mov rdi, input
        mov rsi, IntCnt
        mov rdx, digitCnt
        mov rcx, strLen
        call encriptStr
        mov rdi, input
        call printString
        
    last:   
        mov rax, SYS_exit
        mov rdi, EXIT_SUCCESS
        syscall
    global encriptStr
    encriptStr:
        push    rbx
        push    r12
    ;------get addresses-----------------------
        mov rbx, rdi        ; addr nonaryStr
        mov r12, rsi        ; num
    ;------Set up--------------------------------
        mov     r11, 2          ; multiplier
        mov r10, 0          ; index
        mov r11, 0  
        mov qword [rSum], 0     ; set rSum to 0
        mov rax, 0          ; reset rax
    ;--------Multiplication Loop-------------------------
    multLoop:
        mov al, byte [rbx + r10]        ; get first char
        cmp al, NULL            ; if (char==NULL) DONE
        je  outLoop             ; else keep checking
        cmp al, "0"
        je  calculation
        cmp al, "1"
        je  calculation
        
        jmp outLoop
    calculation:
        sub al, 48              ; al - "0"
        movsx   r11, al
        mov qword [digit], r11      ;store digit into var
        mov rax, qword [rSum]       ;move rSum for Mult
        mul r11
        mov qword [rSum], rax       ; store result into rSum
        mov qword [rSum + 8], rdx
        
        add rax, qword [digit]      ; add digit to running sum
        mov qword [rSum], rax       ; store answer into running sum
        inc r10
        jmp multLoop
    ;----------Mult done----------------------------------------
         outLoop:
        mov rax, qword [rSum]       ; store running sum in rax
        mov qword [r12], rax        ; return limit to address of limit (qword)
         nonary2intDone:
            pop r12
        pop rbx
        ret
    ret
    ;********************************************************************
    ;  Generic function to display a string to the screen.
    ;  String must be NULL terminated.
    ;  Algorithm:
    ;   Count characters in string (excluding NULL)
    ;   Use syscall to output characters
    ; -----
    ;  HLL Call:
    ;   printString(stringAddr);
    ;  Arguments:
    ;   1) address, string
    ;  Returns:
    ;   nothing
    global  printString
    printString:
    ; -----
    ;  Count characters to write.
        mov rdx, 0
    strCountLoop:
        cmp byte [rdi+rdx], NULL
        je  strCountLoopDone
        inc rdx
        jmp strCountLoop
    strCountLoopDone:
        cmp rdx, 0
        je  printStringDone
    ; -----
    ;  Call OS to output string.
        mov rax, SYS_write          ; system code for write()
        mov rsi, rdi            ; address of char to write
        mov rdi, STDOUT         ; file descriptor for std in
                            ; rdx=count to write, set above
        syscall                 ; system call
    ; -----
    ;  String printed, return to calling routine.
    printStringDone:
        ret
    ; ******************************************************************
 
    