assembly - 添加 2 个数字并让程序显示 2 位数字时遇到问题
问题描述
我需要修改这个程序才能打印出一个两位数。输入数字在 1-9 之间,输出是两个个位数数字的和,所以输出应该在 0 到 18 之间。老师的提示是输入应该是 00-18 的格式。我尽我所能翻译了它。
cr equ 13 ; Vognretur
lf equ 10 ; Linjeskift
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
STDERR equ 2
; Datasegment
section .bss
siffer resb 4
; Datasegment
section .data
meld db "Skriv to ensifrede tall skilt med mellomrom.",cr,lf
db "Summen av tallene maa vaere mindre enn 10.",cr,lf
meldlen equ $ - meld
feilmeld db cr,lf, "Skriv kun sifre!",cr,lf
feillen equ $ - feilmeld
crlf db cr,lf
crlflen equ $ - crlf
; Codesegment with program
section .text
global _start
_start:
mov edx,meldlen
mov ecx,meld
mov ebx,STDOUT
mov eax,SYS_WRITE
int 80h
; Read number, read number returns in ecx
; Succesfull return if edx=0
call lessiffer
cmp edx,0 ; Test
jne Slutt
mov eax,ecx ; First digit saved in reg eax
call lessiffer
; Read second digit
; success: edx=0, number in ecx
cmp edx,0 ;Test
jne Slutt
mov ebx,ecx ; second digit saved in reg ebx
call nylinje
add eax,ebx
mov ecx,eax
call skrivsiffer ; Print out value in ecx as a single digit number
Slutt:
mov eax,SYS_EXIT
mov ebx,0
int 80h
; ---------------------------------------------------------
skrivsiffer:
; Prints digit saved in ecx. No value check
push eax
push ebx
push ecx
push edx
add ecx,'0' ; converter tall til ascii.
mov [siffer],ecx
mov ecx,siffer
mov edx,1
mov ebx,STDOUT
mov eax,SYS_WRITE
int 80h
pop edx
pop ecx
pop ebx
pop eax
ret
; ---------------------------------------------------------
lessiffer:
; searches past blank spaces to next non-blank
; Next non-blank is returned ti ecx
push eax
push ebx
Lokke:
; Reads sign from keyboard
mov eax,SYS_READ
mov ebx,STDIN
mov ecx,siffer
mov edx,1
int 80h
mov ecx,[siffer]
cmp ecx,' '
je Lokke
cmp ecx,'0' ; checks if digit is between 0-9
jb Feil
cmp ecx,'9'
ja Feil
sub ecx,'0' ; converts ascii to number.
mov edx,0 ; signals successfull input
pop ebx
pop eax
ret ; Vellykket retur
Feil:
mov edx,feillen
mov ecx,feilmeld
mov ebx,STDERR
mov eax,SYS_WRITE
int 80h
mov edx,1 ; singaled failed input
pop ebx
pop eax
ret ; failed return
; ---------------------------------------------------------
; Move cursor to the left on next line
nylinje:
push eax
push ebx
push ecx
push edx
mov edx,crlflen
mov ecx,crlf
mov ebx,STDOUT
mov eax,SYS_WRITE
int 80h
pop edx
pop ecx
pop ebx
pop eax
ret
; End _start
有很多代码,我知道,但是因为我是汇编新手,我不太确定在哪里修改。我猜想修改将在 skrivsiffer 函数中。
解决方案
推荐阅读
- database - Cassandra 如何在其 SSTables 中高效地对可变长度行进行二进制搜索?
- java - 如何在类路径中添加多个 JAR 文件(通过终端)
- prometheus - 带有多个表达式的警报
- python - Python - 上传文件的名称和缩略图问题
- deep-learning - 我的验证损失增加,而准确性增加。是否过拟合?
- .net-core - simple.odata 和 Microsoft.odata 无法将“@OData.Community.Display.V1.FormattedValue”检索到字典和模型
- javascript - 如何使用axios js使用base64字符串将本地文件上传到IMGBB
- javascript - 数据库最终仅由最后的同一行更新
- android - Android-9 升级:由于 INTERACT_ACROSS_USERS_FULL 和 ACCESS_NETWORK_STATE,PhoneApp 创建失败
- python - 从字符串中提取子字符串和 x 个字符