Extracting digit at end of string assembly 8086 masm











up vote
1
down vote

favorite












I have to extract digit stored in si register. here is my code



lea si, userInput
inc si

mov bx, [si]

mov dx, [si+bx]
add dx, 30h
mov ah, 2h
int 21h


So i think code works fine if i put hard value in dx register like mov dx, [si+2] but if try to use mov dx, [si+bx] it does not work and does not give ouput as expected










share|improve this question
























  • I'm not quite clear on what you are trying to do. But there are a few things I notice. For example you are doing inc si right after your lea. So you are skipping the first byte? Then you do mov bx, [si]. That reads 2 bytes from [si] (since bx is 16bits long). I'm not sure what mov dx, [si+bx] is supposed to do. If the mov from the line above loads 1234 into bx, then this would read the byte at [si + 1234]. That can't be what you want? Then you do mov dx, 30h right before calling int 21. Do you really always intend to print 0? Or did you mean to add 30h?
    – David Wohlferd
    Nov 11 at 5:36












  • sorry I added mov dx, 30h by mistake it should have been add dx, 30h so bx contains the length of the address stored in si and I am using it to print specific index
    – doey doe
    Nov 11 at 13:54















up vote
1
down vote

favorite












I have to extract digit stored in si register. here is my code



lea si, userInput
inc si

mov bx, [si]

mov dx, [si+bx]
add dx, 30h
mov ah, 2h
int 21h


So i think code works fine if i put hard value in dx register like mov dx, [si+2] but if try to use mov dx, [si+bx] it does not work and does not give ouput as expected










share|improve this question
























  • I'm not quite clear on what you are trying to do. But there are a few things I notice. For example you are doing inc si right after your lea. So you are skipping the first byte? Then you do mov bx, [si]. That reads 2 bytes from [si] (since bx is 16bits long). I'm not sure what mov dx, [si+bx] is supposed to do. If the mov from the line above loads 1234 into bx, then this would read the byte at [si + 1234]. That can't be what you want? Then you do mov dx, 30h right before calling int 21. Do you really always intend to print 0? Or did you mean to add 30h?
    – David Wohlferd
    Nov 11 at 5:36












  • sorry I added mov dx, 30h by mistake it should have been add dx, 30h so bx contains the length of the address stored in si and I am using it to print specific index
    – doey doe
    Nov 11 at 13:54













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have to extract digit stored in si register. here is my code



lea si, userInput
inc si

mov bx, [si]

mov dx, [si+bx]
add dx, 30h
mov ah, 2h
int 21h


So i think code works fine if i put hard value in dx register like mov dx, [si+2] but if try to use mov dx, [si+bx] it does not work and does not give ouput as expected










share|improve this question















I have to extract digit stored in si register. here is my code



lea si, userInput
inc si

mov bx, [si]

mov dx, [si+bx]
add dx, 30h
mov ah, 2h
int 21h


So i think code works fine if i put hard value in dx register like mov dx, [si+2] but if try to use mov dx, [si+bx] it does not work and does not give ouput as expected







assembly dos x86-16






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:56

























asked Nov 11 at 3:10









doey doe

94




94












  • I'm not quite clear on what you are trying to do. But there are a few things I notice. For example you are doing inc si right after your lea. So you are skipping the first byte? Then you do mov bx, [si]. That reads 2 bytes from [si] (since bx is 16bits long). I'm not sure what mov dx, [si+bx] is supposed to do. If the mov from the line above loads 1234 into bx, then this would read the byte at [si + 1234]. That can't be what you want? Then you do mov dx, 30h right before calling int 21. Do you really always intend to print 0? Or did you mean to add 30h?
    – David Wohlferd
    Nov 11 at 5:36












  • sorry I added mov dx, 30h by mistake it should have been add dx, 30h so bx contains the length of the address stored in si and I am using it to print specific index
    – doey doe
    Nov 11 at 13:54


















  • I'm not quite clear on what you are trying to do. But there are a few things I notice. For example you are doing inc si right after your lea. So you are skipping the first byte? Then you do mov bx, [si]. That reads 2 bytes from [si] (since bx is 16bits long). I'm not sure what mov dx, [si+bx] is supposed to do. If the mov from the line above loads 1234 into bx, then this would read the byte at [si + 1234]. That can't be what you want? Then you do mov dx, 30h right before calling int 21. Do you really always intend to print 0? Or did you mean to add 30h?
    – David Wohlferd
    Nov 11 at 5:36












  • sorry I added mov dx, 30h by mistake it should have been add dx, 30h so bx contains the length of the address stored in si and I am using it to print specific index
    – doey doe
    Nov 11 at 13:54
















I'm not quite clear on what you are trying to do. But there are a few things I notice. For example you are doing inc si right after your lea. So you are skipping the first byte? Then you do mov bx, [si]. That reads 2 bytes from [si] (since bx is 16bits long). I'm not sure what mov dx, [si+bx] is supposed to do. If the mov from the line above loads 1234 into bx, then this would read the byte at [si + 1234]. That can't be what you want? Then you do mov dx, 30h right before calling int 21. Do you really always intend to print 0? Or did you mean to add 30h?
– David Wohlferd
Nov 11 at 5:36






I'm not quite clear on what you are trying to do. But there are a few things I notice. For example you are doing inc si right after your lea. So you are skipping the first byte? Then you do mov bx, [si]. That reads 2 bytes from [si] (since bx is 16bits long). I'm not sure what mov dx, [si+bx] is supposed to do. If the mov from the line above loads 1234 into bx, then this would read the byte at [si + 1234]. That can't be what you want? Then you do mov dx, 30h right before calling int 21. Do you really always intend to print 0? Or did you mean to add 30h?
– David Wohlferd
Nov 11 at 5:36














sorry I added mov dx, 30h by mistake it should have been add dx, 30h so bx contains the length of the address stored in si and I am using it to print specific index
– doey doe
Nov 11 at 13:54




sorry I added mov dx, 30h by mistake it should have been add dx, 30h so bx contains the length of the address stored in si and I am using it to print specific index
– doey doe
Nov 11 at 13:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Assuming your userInput points at the input structure required for DOS's buffered input function 0Ah, these are my recommandations to correct your code:




  • The second byte holds the number of inputted characters. You mistakenly retrieve this as if it were a word.

  • The input consists of characters that are 1 byte wide. You currently retrieve it as if they were 2 bytes.

  • Since the input is already made up of characters, you don't need to do any conversion on it. (The attempt mov dx, 30h) The fact that this character could represent a (numerical) digit, a letter, a punctuation mark, or anything else, doesn't change this.


Your code then becomes:



lea si, userInput
inc si

mov bl, [si] ;Number of inputted characters
mov bh, 0 ;Need to zero to be able to use the whole address-register BX next

mov dl, [si+bx] ;Retrieve the last inputted character (right before the terminating CR)
mov ah, 02h
int 21h ;Display the character





share|improve this answer





















  • thanks, I think I was putting everything into bx register directly that's why it was giving weird results
    – doey doe
    Nov 11 at 14:05










  • @doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
    – Sep Roland
    Nov 11 at 14:09












  • thanks i will take a brief look
    – doey doe
    Nov 11 at 14:11










  • i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
    – doey doe
    Nov 11 at 14:29












  • @doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
    – Sep Roland
    Nov 11 at 14:36













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',
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%2f53245521%2fextracting-digit-at-end-of-string-assembly-8086-masm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Assuming your userInput points at the input structure required for DOS's buffered input function 0Ah, these are my recommandations to correct your code:




  • The second byte holds the number of inputted characters. You mistakenly retrieve this as if it were a word.

  • The input consists of characters that are 1 byte wide. You currently retrieve it as if they were 2 bytes.

  • Since the input is already made up of characters, you don't need to do any conversion on it. (The attempt mov dx, 30h) The fact that this character could represent a (numerical) digit, a letter, a punctuation mark, or anything else, doesn't change this.


Your code then becomes:



lea si, userInput
inc si

mov bl, [si] ;Number of inputted characters
mov bh, 0 ;Need to zero to be able to use the whole address-register BX next

mov dl, [si+bx] ;Retrieve the last inputted character (right before the terminating CR)
mov ah, 02h
int 21h ;Display the character





share|improve this answer





















  • thanks, I think I was putting everything into bx register directly that's why it was giving weird results
    – doey doe
    Nov 11 at 14:05










  • @doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
    – Sep Roland
    Nov 11 at 14:09












  • thanks i will take a brief look
    – doey doe
    Nov 11 at 14:11










  • i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
    – doey doe
    Nov 11 at 14:29












  • @doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
    – Sep Roland
    Nov 11 at 14:36

















up vote
0
down vote



accepted










Assuming your userInput points at the input structure required for DOS's buffered input function 0Ah, these are my recommandations to correct your code:




  • The second byte holds the number of inputted characters. You mistakenly retrieve this as if it were a word.

  • The input consists of characters that are 1 byte wide. You currently retrieve it as if they were 2 bytes.

  • Since the input is already made up of characters, you don't need to do any conversion on it. (The attempt mov dx, 30h) The fact that this character could represent a (numerical) digit, a letter, a punctuation mark, or anything else, doesn't change this.


Your code then becomes:



lea si, userInput
inc si

mov bl, [si] ;Number of inputted characters
mov bh, 0 ;Need to zero to be able to use the whole address-register BX next

mov dl, [si+bx] ;Retrieve the last inputted character (right before the terminating CR)
mov ah, 02h
int 21h ;Display the character





share|improve this answer





















  • thanks, I think I was putting everything into bx register directly that's why it was giving weird results
    – doey doe
    Nov 11 at 14:05










  • @doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
    – Sep Roland
    Nov 11 at 14:09












  • thanks i will take a brief look
    – doey doe
    Nov 11 at 14:11










  • i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
    – doey doe
    Nov 11 at 14:29












  • @doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
    – Sep Roland
    Nov 11 at 14:36















up vote
0
down vote



accepted







up vote
0
down vote



accepted






Assuming your userInput points at the input structure required for DOS's buffered input function 0Ah, these are my recommandations to correct your code:




  • The second byte holds the number of inputted characters. You mistakenly retrieve this as if it were a word.

  • The input consists of characters that are 1 byte wide. You currently retrieve it as if they were 2 bytes.

  • Since the input is already made up of characters, you don't need to do any conversion on it. (The attempt mov dx, 30h) The fact that this character could represent a (numerical) digit, a letter, a punctuation mark, or anything else, doesn't change this.


Your code then becomes:



lea si, userInput
inc si

mov bl, [si] ;Number of inputted characters
mov bh, 0 ;Need to zero to be able to use the whole address-register BX next

mov dl, [si+bx] ;Retrieve the last inputted character (right before the terminating CR)
mov ah, 02h
int 21h ;Display the character





share|improve this answer












Assuming your userInput points at the input structure required for DOS's buffered input function 0Ah, these are my recommandations to correct your code:




  • The second byte holds the number of inputted characters. You mistakenly retrieve this as if it were a word.

  • The input consists of characters that are 1 byte wide. You currently retrieve it as if they were 2 bytes.

  • Since the input is already made up of characters, you don't need to do any conversion on it. (The attempt mov dx, 30h) The fact that this character could represent a (numerical) digit, a letter, a punctuation mark, or anything else, doesn't change this.


Your code then becomes:



lea si, userInput
inc si

mov bl, [si] ;Number of inputted characters
mov bh, 0 ;Need to zero to be able to use the whole address-register BX next

mov dl, [si+bx] ;Retrieve the last inputted character (right before the terminating CR)
mov ah, 02h
int 21h ;Display the character






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 13:57









Sep Roland

11.4k21843




11.4k21843












  • thanks, I think I was putting everything into bx register directly that's why it was giving weird results
    – doey doe
    Nov 11 at 14:05










  • @doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
    – Sep Roland
    Nov 11 at 14:09












  • thanks i will take a brief look
    – doey doe
    Nov 11 at 14:11










  • i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
    – doey doe
    Nov 11 at 14:29












  • @doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
    – Sep Roland
    Nov 11 at 14:36




















  • thanks, I think I was putting everything into bx register directly that's why it was giving weird results
    – doey doe
    Nov 11 at 14:05










  • @doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
    – Sep Roland
    Nov 11 at 14:09












  • thanks i will take a brief look
    – doey doe
    Nov 11 at 14:11










  • i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
    – doey doe
    Nov 11 at 14:29












  • @doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
    – Sep Roland
    Nov 11 at 14:36


















thanks, I think I was putting everything into bx register directly that's why it was giving weird results
– doey doe
Nov 11 at 14:05




thanks, I think I was putting everything into bx register directly that's why it was giving weird results
– doey doe
Nov 11 at 14:05












@doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
– Sep Roland
Nov 11 at 14:09






@doeydoe You can find some additional info about this DOS input mechanism in another post of mine : stackoverflow.com/questions/47379024/how-buffered-input-works. First chapter in the answer there.
– Sep Roland
Nov 11 at 14:09














thanks i will take a brief look
– doey doe
Nov 11 at 14:11




thanks i will take a brief look
– doey doe
Nov 11 at 14:11












i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
– doey doe
Nov 11 at 14:29






i have one more question i am using this loop to print everything where dx is 1 loop1: mov dx, [si+bx] cmp dx, 1 je match cmp bx, 1 je endLoop sub bx, 1 jmp loop1 match: mov ah, 2h int 21h jmp loop1 endLoop: but it is not entering match loop when dx is 1
– doey doe
Nov 11 at 14:29














@doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
– Sep Roland
Nov 11 at 14:36






@doeydoe Please notice that you need to change all of those DX 's into DL. It's always a single byte! Also I think you want to compare with the character 1, so write cmp dl, '1'
– Sep Roland
Nov 11 at 14:36




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245521%2fextracting-digit-at-end-of-string-assembly-8086-masm%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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine