NASM - The Netwide Assembler
NASM Forum => Example Code => Topic started by: Utkarsha on December 22, 2013, 10:24:45 AM
-
SECTION .data
msg2:db "Enter 3 the numbers: "
len2:equ $-msg2
msg3:db "Sum= "
len3:equ $-msg3
SECTION .bss
count:resb 2
count1:resb 2
num: resd 200
num1: resd 200
result:resb 8
SECTION .text
mov rax,1
mov rdi,1
mov rsi,msg2
mov rdx,len2
syscall
mov rsi,num
mov byte[count1],3
accept:
mov rax,0
mov rdi,1
mov rdx,17
syscall
call convert
add esi,16
dec byte[count1]
jnz accept
mov rax,1
mov rdi,1
mov rsi,msg3
mov rdx,len3
syscall
mov byte[count],3
mov rsi,num1
;mov rdx,0000000000000000H
add:
add rdx,[rsi]
add rsi,16
dec byte[count]
jnz add
call convert_1
mov rax,1
mov rdi,1
mov rsi,result
mov rdx,17
syscall
convert:
mov eax,esi
mov edi,num1
mov byte[count],16
up:
rol rax,4
mov dl,al
and dl,0FH
cmp dl,09H
jbe sub30
sub dl,7H
sub30:
sub dl,30H
mov [edi],dl
inc edi
dec byte[count]
jnz up
ret
convert_1:
mov rax,num1
mov rdi,result
mov byte[count],16
up1:
rol rax,4
mov dl,al
and dl,0FH
cmp dl,09H
jbe add30
add dl,7H
add30:
add dl,30H
mov [rdi],dl
inc rdi
dec byte[count]
jnz up1
ret
mov rax,60
mov rdi,0
syscall
I have tried writing this program to add 3 64bit numbers ,but I'm getting a Segmentation Fault (core dump). I dont know what is going wrong. Please help.
Thanks in advance.
-
I'm guessing this is an assignment?
Anyways,
http://forum.nasm.us/index.php?topic=1792.0
Use the same logic for 64-bit numbers. It's not that hard.
On the other hand, I also noticed
num: resd 200
num1: resd 200
result:resb 8
IMO You don't need THAT much space for just 3 numbers.
And also, result only has 8 bytes allocated. But you're printing a 64bit result. Is this correct?
You're accepting numbers in 17 bytes, but allocate 8 bytes for result?
-
Hi Utkarsha,
I moved your question from "other discussion" here to "example code". It might go better in "programming with Nasm", but we've already got a 32-bit version here...
I put "code tags" around your code. Just the word "code" in square brackets at the beginning of your code, and "/code" at the end.
I can tell you that the segfault is because your sys_exit is in the wrong place. After printing "result", you "fall through" into your "convert" routine. Since it wasn't "call"ed this time, there's no return address on the stack, so when it hits "ret", go boom. Moving sys_exit (right after you print result) fixed that, but I'm still getting wrong result.
I'm not sure I understand what you're doing there. "Convert"ing to hex? I'm going to have to study that...
Oh, yeah - add "global _start" and a "_start:" label for your entrypoint. ld seems to be guessing right, but give it some help...
I'll look at this some more, but you work on it too! Adding some comments about what you intend to do may help you (and us!) figure out where it's going wrong.
Later,
Frank
-
I have made some changes in the given code:
i.e modified for 32 bit numbers and is a 32 bit program(as i have 32 bit o.s). it is for addition of 32 bit hex numbers.
but when i convert the accepted numbers to hex there seems to be a problem.
i cant quite understand it. See if you can modify it a bit more.
Regards,
Manasi