DIV in NASM always return 1












0














There are my all code:



SYS_EXIT  equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1

section .data

msg1 db `xF0x9Fx98x8E`, " Enter the A: "
len1 equ $- msg1

msg2 db `xF0x9Fx98x89`, " Than the B: "
len2 equ $- msg2

msg3 db `xF0x9Fx8DxB0`, " A > B: A / B - 1 = "
len3 equ $- msg3

msg4 db `xF0x9Fx8DxAA`, " A = B: -25"
len4 equ $- msg4

msg5 db `xF0x9Fx8Dx95`, " A < B: (B^3 - 5) / A = "
len5 equ $- msg5


section .bss

a resb 32
b resb 32
x resb 32

section .text
global _start ;must be declared for using gcc

_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, a
mov edx, 32
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, b
mov edx, 32
int 0x80

; Comparing
mov eax, [a]
sub eax, '0'
mov ecx, [b]
sub ecx, '0'

cmp eax, ecx
jg Ab ;A grate than b
je AB ;A and B are equal
jl aB ;a smoller than B

Ab:
cdq
idiv ecx
dec eax

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 32
int 0x80
jmp exit ;go to exit

AB:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, len4
int 0x80
jmp exit ;go to exit

aB:
mov eax, ecx
imul ecx
imul ecx
mov ebx, '5'
sub ebx, '0'
sub eax, ebx

mov ecx, [a]
sub ecx, '0'
idiv ecx

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg5
mov edx, len5
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 1
int 0x80

exit:

mov eax, SYS_EXIT
xor ebx, ebx
int 0x80


In 'a' and 'b' I put different number between 0 and 9 but this operation always return 1. I checked out EDX and noticed that processor subtracts ECX from EAX only one times. In EAX he write 1, and in EDX - remainder from subtraction EAX and ECX.



When I enter a = 9 and b = 4 : EAX after dividing = 1 and EDX = 5. If I enter a = 6 and b = 2 : EAX = 1, EDX = 4



What happened?










share|improve this question
























  • Where are the declarations of a and b?
    – Michael
    Nov 12 '18 at 12:51










  • So what are some example inputs and outputs? And how did you establish that eax always gets the value 1 after the division? I don't see any code that prints the value of eax.
    – Michael
    Nov 12 '18 at 13:20










  • Sorry there mast be don't EDX but EAX. But not the problem. It's I just test registers.
    – Богуслав Павлишинець
    Nov 12 '18 at 13:26










  • processor subtracts ECX from EAX - where?
    – Armali
    Nov 12 '18 at 14:29










  • When I call DIV he write in EDX remainder: EAX - ECX, and in EAX he write 1 (he 1 times subtracts ECX from EAX)
    – Богуслав Павлишинець
    Nov 12 '18 at 14:32
















0














There are my all code:



SYS_EXIT  equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1

section .data

msg1 db `xF0x9Fx98x8E`, " Enter the A: "
len1 equ $- msg1

msg2 db `xF0x9Fx98x89`, " Than the B: "
len2 equ $- msg2

msg3 db `xF0x9Fx8DxB0`, " A > B: A / B - 1 = "
len3 equ $- msg3

msg4 db `xF0x9Fx8DxAA`, " A = B: -25"
len4 equ $- msg4

msg5 db `xF0x9Fx8Dx95`, " A < B: (B^3 - 5) / A = "
len5 equ $- msg5


section .bss

a resb 32
b resb 32
x resb 32

section .text
global _start ;must be declared for using gcc

_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, a
mov edx, 32
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, b
mov edx, 32
int 0x80

; Comparing
mov eax, [a]
sub eax, '0'
mov ecx, [b]
sub ecx, '0'

cmp eax, ecx
jg Ab ;A grate than b
je AB ;A and B are equal
jl aB ;a smoller than B

Ab:
cdq
idiv ecx
dec eax

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 32
int 0x80
jmp exit ;go to exit

AB:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, len4
int 0x80
jmp exit ;go to exit

aB:
mov eax, ecx
imul ecx
imul ecx
mov ebx, '5'
sub ebx, '0'
sub eax, ebx

mov ecx, [a]
sub ecx, '0'
idiv ecx

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg5
mov edx, len5
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 1
int 0x80

exit:

mov eax, SYS_EXIT
xor ebx, ebx
int 0x80


In 'a' and 'b' I put different number between 0 and 9 but this operation always return 1. I checked out EDX and noticed that processor subtracts ECX from EAX only one times. In EAX he write 1, and in EDX - remainder from subtraction EAX and ECX.



When I enter a = 9 and b = 4 : EAX after dividing = 1 and EDX = 5. If I enter a = 6 and b = 2 : EAX = 1, EDX = 4



What happened?










share|improve this question
























  • Where are the declarations of a and b?
    – Michael
    Nov 12 '18 at 12:51










  • So what are some example inputs and outputs? And how did you establish that eax always gets the value 1 after the division? I don't see any code that prints the value of eax.
    – Michael
    Nov 12 '18 at 13:20










  • Sorry there mast be don't EDX but EAX. But not the problem. It's I just test registers.
    – Богуслав Павлишинець
    Nov 12 '18 at 13:26










  • processor subtracts ECX from EAX - where?
    – Armali
    Nov 12 '18 at 14:29










  • When I call DIV he write in EDX remainder: EAX - ECX, and in EAX he write 1 (he 1 times subtracts ECX from EAX)
    – Богуслав Павлишинець
    Nov 12 '18 at 14:32














0












0








0







There are my all code:



SYS_EXIT  equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1

section .data

msg1 db `xF0x9Fx98x8E`, " Enter the A: "
len1 equ $- msg1

msg2 db `xF0x9Fx98x89`, " Than the B: "
len2 equ $- msg2

msg3 db `xF0x9Fx8DxB0`, " A > B: A / B - 1 = "
len3 equ $- msg3

msg4 db `xF0x9Fx8DxAA`, " A = B: -25"
len4 equ $- msg4

msg5 db `xF0x9Fx8Dx95`, " A < B: (B^3 - 5) / A = "
len5 equ $- msg5


section .bss

a resb 32
b resb 32
x resb 32

section .text
global _start ;must be declared for using gcc

_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, a
mov edx, 32
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, b
mov edx, 32
int 0x80

; Comparing
mov eax, [a]
sub eax, '0'
mov ecx, [b]
sub ecx, '0'

cmp eax, ecx
jg Ab ;A grate than b
je AB ;A and B are equal
jl aB ;a smoller than B

Ab:
cdq
idiv ecx
dec eax

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 32
int 0x80
jmp exit ;go to exit

AB:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, len4
int 0x80
jmp exit ;go to exit

aB:
mov eax, ecx
imul ecx
imul ecx
mov ebx, '5'
sub ebx, '0'
sub eax, ebx

mov ecx, [a]
sub ecx, '0'
idiv ecx

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg5
mov edx, len5
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 1
int 0x80

exit:

mov eax, SYS_EXIT
xor ebx, ebx
int 0x80


In 'a' and 'b' I put different number between 0 and 9 but this operation always return 1. I checked out EDX and noticed that processor subtracts ECX from EAX only one times. In EAX he write 1, and in EDX - remainder from subtraction EAX and ECX.



When I enter a = 9 and b = 4 : EAX after dividing = 1 and EDX = 5. If I enter a = 6 and b = 2 : EAX = 1, EDX = 4



What happened?










share|improve this question















There are my all code:



SYS_EXIT  equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1

section .data

msg1 db `xF0x9Fx98x8E`, " Enter the A: "
len1 equ $- msg1

msg2 db `xF0x9Fx98x89`, " Than the B: "
len2 equ $- msg2

msg3 db `xF0x9Fx8DxB0`, " A > B: A / B - 1 = "
len3 equ $- msg3

msg4 db `xF0x9Fx8DxAA`, " A = B: -25"
len4 equ $- msg4

msg5 db `xF0x9Fx8Dx95`, " A < B: (B^3 - 5) / A = "
len5 equ $- msg5


section .bss

a resb 32
b resb 32
x resb 32

section .text
global _start ;must be declared for using gcc

_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, a
mov edx, 32
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80

mov eax, SYS_READ
mov ebx, STDIN
mov ecx, b
mov edx, 32
int 0x80

; Comparing
mov eax, [a]
sub eax, '0'
mov ecx, [b]
sub ecx, '0'

cmp eax, ecx
jg Ab ;A grate than b
je AB ;A and B are equal
jl aB ;a smoller than B

Ab:
cdq
idiv ecx
dec eax

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 32
int 0x80
jmp exit ;go to exit

AB:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg4
mov edx, len4
int 0x80
jmp exit ;go to exit

aB:
mov eax, ecx
imul ecx
imul ecx
mov ebx, '5'
sub ebx, '0'
sub eax, ebx

mov ecx, [a]
sub ecx, '0'
idiv ecx

add eax, '0'
mov [x], eax

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg5
mov edx, len5
int 0x80

mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, x
mov edx, 1
int 0x80

exit:

mov eax, SYS_EXIT
xor ebx, ebx
int 0x80


In 'a' and 'b' I put different number between 0 and 9 but this operation always return 1. I checked out EDX and noticed that processor subtracts ECX from EAX only one times. In EAX he write 1, and in EDX - remainder from subtraction EAX and ECX.



When I enter a = 9 and b = 4 : EAX after dividing = 1 and EDX = 5. If I enter a = 6 and b = 2 : EAX = 1, EDX = 4



What happened?







linux nasm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 14:56

























asked Nov 12 '18 at 12:31









Богуслав Павлишинець

34




34












  • Where are the declarations of a and b?
    – Michael
    Nov 12 '18 at 12:51










  • So what are some example inputs and outputs? And how did you establish that eax always gets the value 1 after the division? I don't see any code that prints the value of eax.
    – Michael
    Nov 12 '18 at 13:20










  • Sorry there mast be don't EDX but EAX. But not the problem. It's I just test registers.
    – Богуслав Павлишинець
    Nov 12 '18 at 13:26










  • processor subtracts ECX from EAX - where?
    – Armali
    Nov 12 '18 at 14:29










  • When I call DIV he write in EDX remainder: EAX - ECX, and in EAX he write 1 (he 1 times subtracts ECX from EAX)
    – Богуслав Павлишинець
    Nov 12 '18 at 14:32


















  • Where are the declarations of a and b?
    – Michael
    Nov 12 '18 at 12:51










  • So what are some example inputs and outputs? And how did you establish that eax always gets the value 1 after the division? I don't see any code that prints the value of eax.
    – Michael
    Nov 12 '18 at 13:20










  • Sorry there mast be don't EDX but EAX. But not the problem. It's I just test registers.
    – Богуслав Павлишинець
    Nov 12 '18 at 13:26










  • processor subtracts ECX from EAX - where?
    – Armali
    Nov 12 '18 at 14:29










  • When I call DIV he write in EDX remainder: EAX - ECX, and in EAX he write 1 (he 1 times subtracts ECX from EAX)
    – Богуслав Павлишинець
    Nov 12 '18 at 14:32
















Where are the declarations of a and b?
– Michael
Nov 12 '18 at 12:51




Where are the declarations of a and b?
– Michael
Nov 12 '18 at 12:51












So what are some example inputs and outputs? And how did you establish that eax always gets the value 1 after the division? I don't see any code that prints the value of eax.
– Michael
Nov 12 '18 at 13:20




So what are some example inputs and outputs? And how did you establish that eax always gets the value 1 after the division? I don't see any code that prints the value of eax.
– Michael
Nov 12 '18 at 13:20












Sorry there mast be don't EDX but EAX. But not the problem. It's I just test registers.
– Богуслав Павлишинець
Nov 12 '18 at 13:26




Sorry there mast be don't EDX but EAX. But not the problem. It's I just test registers.
– Богуслав Павлишинець
Nov 12 '18 at 13:26












processor subtracts ECX from EAX - where?
– Armali
Nov 12 '18 at 14:29




processor subtracts ECX from EAX - where?
– Armali
Nov 12 '18 at 14:29












When I call DIV he write in EDX remainder: EAX - ECX, and in EAX he write 1 (he 1 times subtracts ECX from EAX)
– Богуслав Павлишинець
Nov 12 '18 at 14:32




When I call DIV he write in EDX remainder: EAX - ECX, and in EAX he write 1 (he 1 times subtracts ECX from EAX)
– Богуслав Павлишинець
Nov 12 '18 at 14:32












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262283%2fdiv-in-nasm-always-return-1%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262283%2fdiv-in-nasm-always-return-1%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini