读数并将其更改为二进制
所以我有一个gasemblyx86语言的练习考试
编写一个将加密字符串的void函数,encryptstr()。到 加密字符串,只需在每个字母内添加一个(上部和 小写字母)字符。所有非字母字符(数字, 标点符号,空间等)不变。因此
hello Zoo 12
变为ifmmp [pp 12
。字符串为null终止。功能应该 返回
- 所有字母的总数
- ASCII数字的计数 (“ 0” - “ 9”)和
- 总字符串长度。该功能称为 以下内容:
encryptstr(instry,& ltrcnt,& digitcnt,& strlen);
仪器是输入字符串ltrcnt,digitcnt和strlen是 未签名的双词变量的地址 适当的值(注意,传递的变量未初始化为零)。
您的功能必须使用标准调用约定访问 争论并仅保存/还原所使用的适当寄存器。你 必须定义并显示使用的任何其他数据声明 (如果有)。对于尤其较差或效率低下的点将扣除积分 解决方案。
我自己尝试做 并想知道这是否有意义。
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
; ******************************************************************
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. ThusHello Zoo 12
becomesIfmmp [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
; ******************************************************************
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论