944,205 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 783
  • Assembly RSS
Nov 7th, 2009
0

x86 help with procedures

Expand Post »
I am having a hard time getting my program to execute correctly. it is supposed to take two numbers entered by the user store them in an array, and reverse the array so when added they display correctly. i am to get the numbers using a procedure two seperate times, and reverse the arrays three times (reverse each entered number, and the sum array). furthermore when the user has entered one more than the max number of keys a + or = sign is to be entered for them. eg. i have the computer program set to take 3 numbers for each array so when the user enters a fourth a + or = sign should automatically appear. I cannot get the program to do this, or go any farther. I was hoping someone could let me know where i am going wrong with my code.

here it is:

Assembly Syntax (Toggle Plain Text)
  1.  
  2. BOZOS_CODE SEGMENT ;The "logical" segment is named BOZOS_CODE
  3. ASSUME CS:BOZOS_CODE ;The "assume" statement tells the assembler
  4. ; that BOZOS_CODE is a code segment (CS)
  5. ORG 100h ;In case we decide to convert the program
  6. ; to a COM file (makes 256 "empty" locations)
  7.  
  8. START: jmp begin ;Since this is a code segment the first non-
  9. ; directive item must be an instruction. We
  10. ; jump around our data area
  11.  
  12.  
  13. num_of_bytes EQU 3
  14. first_num db num_of_bytes dup (0), '$'
  15. second_num db num_of_bytes dup (0), '$'
  16. the_sum db num_of_bytes + 1 dup (0), '$'
  17. message_1 db 'Enter the first multi-digit number (up to 12 digits in length) followed by a'
  18. db '"+" symbol then enter a second multi-digit number (up to 12 digits in '
  19. db 'length) followed by a "=" symbol $'
  20.  
  21.  
  22.  
  23. key_stroke db 'x$'
  24. operand db 'x$'
  25.  
  26.  
  27.  
  28. GET_NUM: mov ah, 00h ;Read keyboard function
  29. int 16h ;Call BIOS (return occurs when key is pressed)
  30. ;AL now contains the ASCII code of key pressed
  31.  
  32. mov key_stroke, al
  33.  
  34. mov ah, 09h ;Display string function
  35. mov dx, offset key_stroke ;Pass the starting address of string
  36. int 21h ;Call DOS
  37.  
  38. mov al, key_stroke
  39.  
  40. cmp al, cl
  41. jne continue
  42. ret ;return
  43.  
  44. continue: and al, 0fh
  45.  
  46. mov [BX], al
  47.  
  48. inc BX
  49.  
  50. cmp [bx], ch
  51.  
  52. jbe get_num
  53.  
  54. mov operand, cl
  55.  
  56. mov ah, 09h ;Display string function
  57. mov dx, offset operand ;Pass the starting address of string
  58.  
  59. int 21h ;Call DOS
  60. ret ;return
  61.  
  62. REV_NUM: mov cl, [bx]
  63.  
  64. mov ch, [di]
  65.  
  66. mov [di], cl
  67.  
  68. mov [bx], ch
  69.  
  70. dec BX
  71. inc di
  72.  
  73. cmp di, bx
  74.  
  75. jae rev_num
  76. ret
  77.  
  78.  
  79. begin: mov ax, cs
  80. mov ds, ax
  81.  
  82. mov cl, 0
  83.  
  84. mov ah, 06h ;Scroll-up window function
  85. mov al, 0 ; Clear entire window
  86. mov bh, 1ch ; Display "attribute byte" for cleared area
  87. ; Blue background/Red characters
  88. ; Bit definitions: Blink|Background R,G,B|
  89. ; Intensity|Character R,G,B
  90. mov ch, 0 ; Y = 0 (Corner coordinates of window
  91. mov cl, 0 ; X = 0 to clear)
  92. mov dh, 24 ; Y = 24
  93. mov dl, 85 ; X = 79
  94. int 10h ;Call BIOS
  95.  
  96.  
  97. mov ah, 02h ;Set cursor position function
  98. mov bh, 0 ; Display page 0
  99. mov dh, 0 ; Y position 25 rows (Y: 0 -> 24)
  100. mov dl, 0 ; X position 80 columns (X: 0 -> 79)
  101. int 10h ;Call BIOS
  102.  
  103.  
  104. mov ah, 09h ;Display string function
  105. mov dx, offset message_1 ;Pass the starting offset of the
  106. ; string to be displayed
  107. ; (the string to be displayed starts
  108. ; at the label message (in the data
  109. ; area) and ends with a '$')
  110. ;Note: DS must point to the beginning
  111. ; of the segment containing the
  112. ; string to be displayed, e.g.,
  113. ; mov ax, cs (usually done at
  114. ; mov ds, ax the beginning of
  115. ; the program)
  116. ;Note: This display procedure updates
  117. ; the cursor position as each
  118. ; character is displayed
  119. int 21h ;Call DOS
  120.  
  121. mov bx, offset first_num
  122. mov cl, '+'
  123. mov ch, num_of_bytes
  124.  
  125. call get_num
  126.  
  127. mov bx, offset second_num
  128. mov cl, '='
  129. mov ch, num_of_bytes ;do i need to do this again?
  130.  
  131. call get_num
  132.  
  133. mov di, offset first_num
  134.  
  135. call rev_num
  136.  
  137. mov di, offset second_num
  138.  
  139. call rev_num
  140.  
  141. mov BX, 0 ;Index set to zero now
  142. mov ah, 0 ;ah now used for carry, and is set to zero
  143.  
  144. adder: mov al, first_num[BX]
  145. add al, second_num[BX]
  146. add al, ah
  147.  
  148. cmp al, 9
  149. jbe no_carry
  150. mov ah, 1
  151. sub al, 10
  152. jmp make_ascii
  153.  
  154. no_carry: mov ah, 0
  155.  
  156.  
  157. make_ascii: or al, 30h
  158.  
  159. mov the_sum[BX], al
  160. inc BX
  161.  
  162. cmp BX, num_of_bytes
  163. je fix_carry ; if array is full jump to fix carry character
  164. jmp adder
  165.  
  166. fix_carry: or ah, 30h
  167.  
  168. mov the_sum[BX], ah
  169.  
  170. mov bx, num_of_bytes - 1
  171. mov di, offset the_sum
  172.  
  173. call rev_num
  174.  
  175. ;to display sum
  176.  
  177. display: mov ah, 09h ;Display string function
  178. mov dx, offset the_sum ;Pass the starting address of string
  179. int 21h ;Call DOS
  180.  
  181. ;To exit to DOS
  182.  
  183. exit: mov ah, 4ch ;Exit to DOS function
  184. mov al, 0 ;ERRORLEVEL = 0
  185. int 21h ;Call DOS
  186.  
  187.  
  188.  
  189. BOZOS_CODE ENDS
  190. END START ;You MUST have a carriage return after START

thank you
Similar Threads
Reputation Points: 39
Solved Threads: 0
Light Poster
RobBrown is offline Offline
47 posts
since Jul 2009
Nov 7th, 2009
0
Re: x86 help with procedures
I wrote up an NASM source with similar functions to your
own code. A function for reversing the array, for
adding the two arrays as unpacked BCS, and for entering
values are here. Call to array reversal is commented out.
After three characters have been read in either a + or =
sign is displayed, numbers are added, result is printed.

Assembly Syntax (Toggle Plain Text)
  1. bits 16
  2. org 100h
  3.  
  4. jmp Ldead_stag
  5. glass times 0x3 db 0x0
  6. neck times 0x3 db 0x0
  7. withme times 0x4 db 0x0
  8. Ldead_stag:
  9. mov bx, glass ; our first sequence of
  10. ; arbitrary digits from user
  11. mov dl, '+' ; a maltese cross character, plus
  12. call stolen_dec
  13. mov bx, neck ; our second array
  14. mov dl, '=' ; equality symbol
  15. call stolen_dec
  16. ;mov bx, glass
  17. ;call permutate
  18. call sum_dumb
  19. call flashed_sum
  20. ret
  21.  
  22. stolen_dec: ; DL-A + or = with
  23. ; permission BX-points to array
  24. push dx
  25. mov cx, 3
  26. stolen_dec_talks:
  27. mov ah, 0x1
  28. int 0x21
  29. and al, 0xf
  30. mov [bx], al
  31. inc bx
  32. dec cx
  33. jnz stolen_dec_talks
  34. pop dx
  35. mov ah, 0x2
  36. int 0x21
  37. ret
  38.  
  39. sum_dumb:
  40. mov bx, glass+2
  41. mov si, neck+2
  42. mov di, withme+3
  43. xor ah, ah
  44. mov cx, 3
  45. sum_dumb_l:
  46. mov al, [bx]
  47. add al, [si]
  48. aaa
  49. cmp ah, 0
  50. jnz sum_dumb_addend
  51. sum_dumb_imback:
  52. add al, [di]
  53. mov [di], al
  54. dec bx
  55. dec si
  56. dec di
  57. dec cx
  58. jnz sum_dumb_l
  59. ret
  60.  
  61. sum_dumb_addend:
  62. mov [di-1], ah
  63. xor ah, ah
  64. jmp sum_dumb_imback
  65.  
  66. flashed_sum:
  67. mov bx, withme
  68. mov cx, 4
  69. flashed_sum_again:
  70. mov dl, [bx]
  71. add dl, 0x30
  72. mov ah, 2
  73. int 0x21
  74. inc bx
  75. dec cx
  76. jnz flashed_sum_again
  77. ret
  78.  
  79. permutate: ; a array reversal BX=array
  80. ; assumes 3 byte array
  81. mov di, bx
  82. add bx, 2
  83. mov cx, 2
  84. permutate_again:
  85. mov al, [di]
  86. mov dl, [bx]
  87. mov [bx], al
  88. mov [di], dl
  89. inc di
  90. dec bx
  91. dec cx
  92. jnz permutate_again
  93. ret
Reputation Points: 36
Solved Threads: 19
Junior Poster
NotNull is offline Offline
198 posts
since Oct 2008
Nov 7th, 2009
0
Re: x86 help with procedures
thanks it helps to see how you did it, im still having a hard time getting it to let me enter the 4th character and then forcing a + or =. My instructor wants us to have it do this:

"After the user has entered the maximum number of digits, the procedure should force the display of the termination character (a passed parameter) whether the user enters it or not as the next keypress.

In other words, the user might expect to enter the maximum number of digits allowed and then enter the termination character. To accommodate this, the procedure should wait for another keypress after the maximum number of digits has been entered (and stored in the array). After the user enters the next keypress the procedure should display the termination character and exit (i.e., return)."

I have tried several different ways, but i am just stumped...
Reputation Points: 39
Solved Threads: 0
Light Poster
RobBrown is offline Offline
47 posts
since Jul 2009
Nov 8th, 2009
0
Re: x86 help with procedures
I'm sorry I didn't know that you were required to let the
user enter a fourth character.
So they can enter a variable number of chars up to the
maximum amount,
and always use a termination character to indicate end
of input.
Also my code always expects three characters to be read
So thats why you had the reversal procedure,
cuz the number of characters read is not always three.
so
12
123
21
321
Thats neat.
Reputation Points: 36
Solved Threads: 19
Junior Poster
NotNull is offline Offline
198 posts
since Oct 2008
Nov 8th, 2009
0
Re: x86 help with procedures
ya its pretty cool, the arrays are then reversed and added into a sum array. then the sum array is reversed and the correct answer comes out. the original program without procedures works perfect but to break it down into using procedures gets tricky.
Reputation Points: 39
Solved Threads: 0
Light Poster
RobBrown is offline Offline
47 posts
since Jul 2009
Nov 8th, 2009
0
Re: x86 help with procedures
That's actually kind of funny, I usually have a hard time
not breaking my programs down into procedures.
I think it is essential to know what pieces of code
to encapsulate into procedures and how certain
levels of procedural abstraction should be performed.
Breaking some of your program's source code down into
procedures is only good practice, exercise,
wouldn't you say?

I don't know how different mine is from yours
now, I tried to translate your code to my assembler
and just said @^ck it.
But it will wait for an extra key press when max
chars are read, and will now read a variable number
of characters instead of three like the last I showed
you.
Termination character is 0xd Carriage Return.

Assembly Syntax (Toggle Plain Text)
  1. bits 16
  2. org 100h
  3.  
  4. jmp Ldead_stag
  5. glass times 0x3 db 0x0
  6. neck times 0x3 db 0x0
  7. withme times 0x4 db 0x0
  8. tmp times 0x3 db 0x0
  9. Ldead_stag:
  10. mov bx, glass ; our first sequence of
  11. ; arbitrary digits from user
  12. mov dl, '+' ; a maltese cross character, plus
  13. call stolen_dec
  14. mov bx, neck ; our second array
  15. mov dl, '=' ; equality symbol
  16. call stolen_dec
  17. mov bx, glass
  18. call permutate
  19. mov bx, neck
  20. call permutate
  21. call sum_dumb
  22. mov bx, withme
  23. call permutate_b
  24. call flashed_sum
  25. ret
  26.  
  27. stolen_dec: ; DL-A + or = with permission BX-points to array
  28. push dx
  29. push bx
  30. mov cx, 3
  31. xor dx, dx
  32. stolen_dec_talks:
  33. ;mov ah, 0x1
  34. ;int 0x21
  35. ;cmp al, 0x1b
  36. call getkey
  37. jc stolen_dec_helps
  38. and al, 0xf
  39. mov [bx], al
  40. inc dx
  41. inc bx
  42. dec cx
  43. jnz stolen_dec_talks
  44. push dx
  45. stolen_dec_walks:
  46. mov ah, 6
  47. mov dl, 0xff
  48. int 0x21
  49. jz stolen_dec_walks
  50. pop dx
  51. stolen_dec_helps:
  52. mov bp, sp
  53. call fixup
  54. pop bx
  55. pop dx
  56. mov ah, 0x2
  57. int 0x21
  58. ret
  59.  
  60. fixup:
  61. cmp dx, 0
  62. jz fixup_e
  63. cmp dx, 3
  64. jz fixup_e
  65. mov si, [bp]
  66. mov di, tmp
  67. mov cx, dx
  68. cld
  69. rep movsb
  70. mov di, [bp]
  71. mov si, tmp
  72. mov cx, 3
  73. sub cx, dx
  74. add di, cx
  75. mov cx, dx
  76. rep movsb
  77. cmp dx, 2
  78. jz fixup_1
  79. cmp dx, 1
  80. jz fixup_2
  81. fixup_e:
  82. ret
  83. fixup_2:
  84. mov bx, [bp]
  85. mov word [bx], 0
  86. ret
  87. fixup_1:
  88. mov bx, [bp]
  89. mov byte [bx], 0
  90. ret
  91.  
  92. sum_dumb:
  93. mov bx, glass
  94. mov si, neck
  95. mov di, withme
  96. xor ah, ah
  97. mov cx, 3
  98. sum_dumb_l:
  99. mov al, [bx]
  100. add al, [si]
  101. aaa
  102. cmp ah, 0
  103. jnz sum_dumb_addend
  104. sum_dumb_imback:
  105. add al, [di]
  106. mov [di], al
  107. inc bx
  108. inc si
  109. inc di
  110. dec cx
  111. jnz sum_dumb_l
  112. ret
  113.  
  114. sum_dumb_addend:
  115. mov [di+1], ah
  116. xor ah, ah
  117. jmp sum_dumb_imback
  118.  
  119. flashed_sum:
  120. mov bx, withme
  121. mov cx, 4
  122. flashed_sum_again:
  123. mov dl, [bx]
  124. add dl, 0x30
  125. mov ah, 2
  126. int 0x21
  127. inc bx
  128. dec cx
  129. jnz flashed_sum_again
  130. ret
  131.  
  132. permutate: ; a array reversal BX=array assumes 3 byte array
  133. mov di, bx
  134. add bx, 2
  135. mov cx, 2
  136. permutate_again:
  137. mov al, [di]
  138. mov dl, [bx]
  139. mov [bx], al
  140. mov [di], dl
  141. inc di
  142. dec bx
  143. dec cx
  144. jnz permutate_again
  145. ret
  146.  
  147. permutate_b: ; a array reversal BX=array assumes 3 byte array
  148. mov di, bx
  149. add bx, 3
  150. mov cx, 2
  151. permutate_b_again:
  152. mov al, [di] ; begin
  153. mov dl, [bx] ; end
  154. mov [bx], al
  155. mov [di], dl
  156. inc di
  157. dec bx
  158. dec cx
  159. jnz permutate_b_again
  160. ret
  161.  
  162. getkey:
  163. push dx
  164. getkey_l:
  165. mov ah, 6
  166. mov dl, 0xff
  167. int 0x21
  168. jz getkey_l
  169. cmp al, 0xd
  170. jz getkey_ignore
  171. mov dl, al
  172. mov ah, 2
  173. int 0x21
  174. pop dx
  175. ret
  176. getkey_ignore:
  177. stc
  178. pop dx
  179. ret
Last edited by NotNull; Nov 8th, 2009 at 6:03 am.
Reputation Points: 36
Solved Threads: 19
Junior Poster
NotNull is offline Offline
198 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: I need halp assembing my program [NASM + NASM - IDE]
Next Thread in Assembly Forum Timeline: Trying to analyse code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC