5.3 trace loop programs implemented by loop commands in debug

Source: Internet
Author: User

Problem: Calculate FFFF: multiply the number in unit 0006 by 3, and the result is stored in dx.

Answer:

I. Analysis ideas:

1. Will the result after calculation exceed the range that DX can store? FFFF: The book in unit 0006 is a byte data with 8 bits. The value ranges from 0 to the eight power of 2, and the maximum value is the eight power of 2, that is, 255. The value after it is multiplied by 3 is 255 × 3 = 765 = 02fdh. The maximum value that DX can store is the 16 power of 2, that is, 65535 = 0 ffffh. It is obvious that 02fdh is smaller than 0ffffh. So the conclusion is that it can be stored in dx.

2. Multiply it by 3, which is equivalent to adding it three times. You can use a loop. You can put the value of FFFF: 0006 in ax first, but the data in FFFF: 0006 is 8 bits, while ax is 16 bits. Therefore, the high position of ax should be cleared. Second, it takes three cycles to send 3 to CX.

Ii. source code writing:

Assume Cs: codesg <br/> codesg segment <br/> // put data in units of FFFF: 0006 into ax <br/> mov BX, 0 ffffh <br/> mov ds, BX <br/> mov ax, [0006 H] <br/> // The next step is to clear the upper and middle bits of ax <br/> mov ah, 0 h // so that the 0 FFFF: the content in 0006 is sent to ax </P> <p> // The number of cycles 3 is sent to CX <br/> mov CX, 3 H </P> <p> // put 0 into DX <br/> mov dx, 0 h <br/> // 3 cycles <br/> S: Add dx, ax <br/> loop S </P> <p> // The program returns <br/> mov ax, 4c00h <br/> int 21 h <br/> codesg ends <br/> end </P> <p>

Iii. Compile the connection process as follows:

Run masm.exe and enter the source code path.

Microsoft (r) macro proceser Version 5.00 <br/> copyright (c) Microsoft Corp 1981-1985,198 7. all rights reserved. </P> <p> source filename [. ASM]: C:/ASSEMBLY/mycode. ASM <br/> Object filename [mycode. OBJ]: <br/> source listing [NUL. LST]: <br/> cross-reference [NUL. CRF]: <br/> 50428 + 450452 bytes symbol space free </P> <p> 0 warning errors <br/> 0 severe errors <br/>

Run link.exe and enter the OBJ file to be connected.

Microsoft (r) Overlay linker Version 3.60 <br/> copyright (c) Microsoft Corp 1983-1987. all rights reserved. </P> <p> Object modules [. OBJ]: mycode <br/> RUN file [mycode. EXE]: <br/> list file [NUL. MAP]: <br/> libraries [. lib]: <br/> link: Warning l4021: no stack segment <br/>

3. the debugging process is as follows:

Ax = 0065 BX = FFFF Cx = 0000 dx = 0000 sp = fffe BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 000a NV up ei pl nz Na Po NC <br/> 0100: 000a b90300 mov CX, 00003 H <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0003 dx = 0000 sp = fffe BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 000d NV up ei pl nz Na Po NC <br/> 0100: 000d ba0000 mov dx, 00000 H <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0003 dx = 0000 sp = fffe BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0010 NV up ei pl nz Na Po NC <br/> 0100:0010 03d0 add dx, ax <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0003 dx = 0065 sp = fffe BP = 0000 Si = 0000 di = 0000 <br /> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0012 NV up ei pl nz na pe nc <br/> 0100:0012 e2fc loop 010 H <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0002 dx = 0065 sp = fffe BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0010 NV up ei pl nz na pe nc <br/> 0100:0010 03d0 add dx, ax <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0002 dx = 00ca sp = fffe BP = 0000 Si = 0000 di = 0000 <br /> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0012 NV up ei pl nz na pe nc <br/> 0100:0012 e2fc loop 010 H <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0001 dx = 00ca sp = fffe BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0010 NV up ei pl nz na pe nc <br/> 0100:0010 03d0 add dx, ax <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0001 dx = 012f sp = fffe BP = 0000 Si = 0000 di = 0000 <br /> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0012 NV up ei pl nz Na Po NC <br/> 0100:0012 e2fc loop 010 H <br/>-T </P> <p> AX = 0065 BX = FFFF Cx = 0000 dx = 012f sp = fffe BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0014 NV up ei pl nz Na Po NC <br/> 0100:0014 b8004c mov ax, 04c00h <br/>-T </P> <p> AX = 4c00 BX = FFFF Cx = 0000 dx = 012f sp = fffe BP = 0000 Si = 0000 di = 0000 <br /> DS = FFFF es = 0100 Ss = 0100 cs = 0100 IP = 0017 NV up ei pl nz Na Po NC <br/> 0100:0017 cd21 int 021 H <br/>-P </P> <p> AX = 4c00 BX = FFFF Cx = 0000 dx = 012f sp = fff8 BP = 0000 Si = 0000 di = 0000 <br/> DS = FFFF es = 0100 Ss = 0100 cs = f400 IP = 0204 NV up di pl nz Na Po NC <br/> f400: 0204 CF iret <br/>

The result is 012fh. If we want to skip the first few steps and directly jump to the out-of-loop execution, we can first check the memory unit address generated by the loop, for example, CS: 0010, the command g 0010 can be used. When the Enter key is pressed, the program will execute add Dx and ax to stop and wait for us to start debugging. If you want to run the loop once, you can use the p command to achieve the goal. Of course, you can use g 0014 to achieve the same purpose.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.